<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class Socket - socket: Ruby Standard Library Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#class-Socket-label-What-27s+a+socket-3F">What’s a socket?</a>
    <li><a href="#class-Socket-label-Quick+start">Quick start</a>
    <li><a href="#class-Socket-label-Exception+Handling">Exception Handling</a>
    <li><a href="#class-Socket-label-Convenience+methods">Convenience methods</a>
    <li><a href="#class-Socket-label-Documentation+by">Documentation by</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link"><a href="BasicSocket.html">BasicSocket</a>
</div>

        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-accept_loop">::accept_loop</a>
    <li ><a href="#method-c-getaddrinfo">::getaddrinfo</a>
    <li ><a href="#method-c-gethostbyaddr">::gethostbyaddr</a>
    <li ><a href="#method-c-gethostbyname">::gethostbyname</a>
    <li ><a href="#method-c-gethostname">::gethostname</a>
    <li ><a href="#method-c-getifaddrs">::getifaddrs</a>
    <li ><a href="#method-c-getnameinfo">::getnameinfo</a>
    <li ><a href="#method-c-getservbyname">::getservbyname</a>
    <li ><a href="#method-c-getservbyport">::getservbyport</a>
    <li ><a href="#method-c-ip_address_list">::ip_address_list</a>
    <li ><a href="#method-c-new">::new</a>
    <li ><a href="#method-c-pack_sockaddr_in">::pack_sockaddr_in</a>
    <li ><a href="#method-c-pack_sockaddr_un">::pack_sockaddr_un</a>
    <li ><a href="#method-c-pair">::pair</a>
    <li ><a href="#method-c-sockaddr_in">::sockaddr_in</a>
    <li ><a href="#method-c-sockaddr_un">::sockaddr_un</a>
    <li ><a href="#method-c-socketpair">::socketpair</a>
    <li ><a href="#method-c-tcp">::tcp</a>
    <li ><a href="#method-c-tcp_server_loop">::tcp_server_loop</a>
    <li ><a href="#method-c-tcp_server_sockets">::tcp_server_sockets</a>
    <li ><a href="#method-c-udp_server_loop">::udp_server_loop</a>
    <li ><a href="#method-c-udp_server_loop_on">::udp_server_loop_on</a>
    <li ><a href="#method-c-udp_server_recv">::udp_server_recv</a>
    <li ><a href="#method-c-udp_server_sockets">::udp_server_sockets</a>
    <li ><a href="#method-c-unix">::unix</a>
    <li ><a href="#method-c-unix_server_loop">::unix_server_loop</a>
    <li ><a href="#method-c-unix_server_socket">::unix_server_socket</a>
    <li ><a href="#method-c-unix_socket_abstract_name-3F">::unix_socket_abstract_name?</a>
    <li ><a href="#method-c-unpack_sockaddr_in">::unpack_sockaddr_in</a>
    <li ><a href="#method-c-unpack_sockaddr_un">::unpack_sockaddr_un</a>
    <li ><a href="#method-i-accept">#accept</a>
    <li ><a href="#method-i-accept_nonblock">#accept_nonblock</a>
    <li ><a href="#method-i-bind">#bind</a>
    <li ><a href="#method-i-connect">#connect</a>
    <li ><a href="#method-i-connect_nonblock">#connect_nonblock</a>
    <li ><a href="#method-i-ipv6only-21">#ipv6only!</a>
    <li ><a href="#method-i-listen">#listen</a>
    <li ><a href="#method-i-recvfrom">#recvfrom</a>
    <li ><a href="#method-i-recvfrom_nonblock">#recvfrom_nonblock</a>
    <li ><a href="#method-i-sysaccept">#sysaccept</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-Socket">
    <h1 id="class-Socket" class="class">
      class Socket
    </h1>

    <section class="description">
    
<p>Class <code>Socket</code> provides access to the underlying operating system socket implementations.  It can be used to provide more operating system specific functionality than the protocol-specific socket classes.</p>

<p>The constants defined under <a href="Socket/Constants.html"><code>Socket::Constants</code></a> are also defined under <a href="Socket.html"><code>Socket</code></a>.  For example, <a href="Socket.html#AF_INET"><code>Socket::AF_INET</code></a> is usable as well as Socket::Constants::AF_INET.  See <a href="Socket/Constants.html"><code>Socket::Constants</code></a> for the list of constants.</p>

<h3 id="class-Socket-label-What-27s+a+socket-3F">What’s a socket?<span><a href="#class-Socket-label-What-27s+a+socket-3F">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Sockets are endpoints of a bidirectional communication channel. Sockets can communicate within a process, between processes on the same machine or between different machines.  There are many types of socket: <a href="TCPSocket.html"><code>TCPSocket</code></a>, <a href="UDPSocket.html"><code>UDPSocket</code></a> or <a href="UNIXSocket.html"><code>UNIXSocket</code></a> for example.</p>

<p>Sockets have their own vocabulary:</p>

<p><strong>domain:</strong> The family of protocols:</p>
<ul><li>
<p><a href="Socket.html#PF_INET"><code>Socket::PF_INET</code></a></p>
</li><li>
<p><a href="Socket.html#PF_INET6"><code>Socket::PF_INET6</code></a></p>
</li><li>
<p><a href="Socket.html#PF_UNIX"><code>Socket::PF_UNIX</code></a></p>
</li><li>
<p>etc.</p>
</li></ul>

<p><strong>type:</strong> The type of communications between the two endpoints, typically</p>
<ul><li>
<p><a href="Socket.html#SOCK_STREAM"><code>Socket::SOCK_STREAM</code></a></p>
</li><li>
<p><a href="Socket.html#SOCK_DGRAM"><code>Socket::SOCK_DGRAM</code></a>.</p>
</li></ul>

<p><strong>protocol:</strong> Typically <em>zero</em>. This may be used to identify a variant of a protocol.</p>

<p><strong>hostname:</strong> The identifier of a network interface:</p>
<ul><li>
<p>a string (hostname, IPv4 or IPv6 address or <code>broadcast</code> which specifies a broadcast address)</p>
</li><li>
<p>a zero-length string which specifies <a href="Socket.html#INADDR_ANY"><code>INADDR_ANY</code></a></p>
</li><li>
<p>an integer (interpreted as binary address in host byte order).</p>
</li></ul>

<h3 id="class-Socket-label-Quick+start">Quick start<span><a href="#class-Socket-label-Quick+start">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Many of the classes, such as <a href="TCPSocket.html"><code>TCPSocket</code></a>, <a href="UDPSocket.html"><code>UDPSocket</code></a> or <a href="UNIXSocket.html"><code>UNIXSocket</code></a>, ease the use of sockets comparatively to the equivalent C programming interface.</p>

<p>Let’s create an internet socket using the IPv4 protocol in a C-like manner:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-identifier">s</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">SOCK_STREAM</span>
<span class="ruby-identifier">s</span>.<span class="ruby-identifier">connect</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&#39;example.com&#39;</span>)
</pre>

<p>You could also use the <a href="TCPSocket.html"><code>TCPSocket</code></a> class:</p>

<pre class="ruby"><span class="ruby-identifier">s</span> = <span class="ruby-constant">TCPSocket</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;example.com&#39;</span>, <span class="ruby-value">80</span>
</pre>

<p>A simple server might look like this:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-identifier">server</span> = <span class="ruby-constant">TCPServer</span>.<span class="ruby-identifier">new</span> <span class="ruby-value">2000</span> <span class="ruby-comment"># Server bound to port 2000</span>

<span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">client</span> = <span class="ruby-identifier">server</span>.<span class="ruby-identifier">accept</span>    <span class="ruby-comment"># Wait for a client to connect</span>
  <span class="ruby-identifier">client</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;Hello !&quot;</span>
  <span class="ruby-identifier">client</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;Time is #{Time.now}&quot;</span>
  <span class="ruby-identifier">client</span>.<span class="ruby-identifier">close</span>
<span class="ruby-keyword">end</span>
</pre>

<p>A simple client may look like this:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-identifier">s</span> = <span class="ruby-constant">TCPSocket</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;localhost&#39;</span>, <span class="ruby-value">2000</span>

<span class="ruby-keyword">while</span> <span class="ruby-identifier">line</span> = <span class="ruby-identifier">s</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment"># Read lines from socket</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-identifier">line</span>         <span class="ruby-comment"># and print them</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">s</span>.<span class="ruby-identifier">close</span>             <span class="ruby-comment"># close socket when done</span>
</pre>

<h3 id="class-Socket-label-Exception+Handling">Exception Handling<span><a href="#class-Socket-label-Exception+Handling">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Ruby’s <a href="Socket.html"><code>Socket</code></a> implementation raises exceptions based on the error generated by the system dependent implementation.  This is why the methods are documented in a way that isolate Unix-based system exceptions from Windows based exceptions. If more information on a particular exception is needed, please refer to the Unix manual pages or the Windows WinSock reference.</p>

<h3 id="class-Socket-label-Convenience+methods">Convenience methods<span><a href="#class-Socket-label-Convenience+methods">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Although the general way to create socket is <a href="Socket.html#method-c-new"><code>Socket.new</code></a>, there are several methods of socket creation for most cases.</p>
<dl class="rdoc-list note-list"><dt>TCP client socket
<dd>
<p><a href="Socket.html#method-c-tcp"><code>Socket.tcp</code></a>, TCPSocket.open</p>
</dd><dt>TCP server socket
<dd>
<p><a href="Socket.html#method-c-tcp_server_loop"><code>Socket.tcp_server_loop</code></a>, TCPServer.open</p>
</dd><dt>UNIX client socket
<dd>
<p><a href="Socket.html#method-c-unix"><code>Socket.unix</code></a>, UNIXSocket.open</p>
</dd><dt>UNIX server socket
<dd>
<p><a href="Socket.html#method-c-unix_server_loop"><code>Socket.unix_server_loop</code></a>, UNIXServer.open</p>
</dd></dl>

<h3 id="class-Socket-label-Documentation+by">Documentation by<span><a href="#class-Socket-label-Documentation+by">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p>Zach Dennis</p>
</li><li>
<p>Sam Roberts</p>
</li><li>
<p><em>Programming Ruby</em> from The Pragmatic Bookshelf.</p>
</li></ul>

<p>Much material in this documentation is taken with permission from <em>Programming Ruby</em> from The Pragmatic Bookshelf.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="AF_ALG">AF_ALG
          <dd><p>Interface to kernel crypto API</p>
          <dt id="AF_APPLETALK">AF_APPLETALK
          <dd><p>AppleTalk protocol</p>
          <dt id="AF_ATM">AF_ATM
          <dd><p>Asynchronous Transfer Mode</p>
          <dt id="AF_AX25">AF_AX25
          <dd><p>AX.25 protocol</p>
          <dt id="AF_BLUETOOTH">AF_BLUETOOTH
          <dd><p>Bluetooth low-level socket protocol</p>
          <dt id="AF_CAN">AF_CAN
          <dd><p>Controller Area Network automotive bus protocol</p>
          <dt id="AF_CCITT">AF_CCITT
          <dd><p>CCITT (now ITU-T) protocols</p>
          <dt id="AF_CHAOS">AF_CHAOS
          <dd><p>MIT CHAOS protocols</p>
          <dt id="AF_CNT">AF_CNT
          <dd><p>Computer Network Technology</p>
          <dt id="AF_COIP">AF_COIP
          <dd><p>Connection-oriented IP</p>
          <dt id="AF_DATAKIT">AF_DATAKIT
          <dd><p>Datakit protocol</p>
          <dt id="AF_DEC">AF_DEC
          <dd><p>DECnet protocol</p>
          <dt id="AF_DECnet">AF_DECnet
          <dd><p>DECnet protocol</p>
          <dt id="AF_DLI">AF_DLI
          <dd><p>DEC Direct Data Link Interface protocol</p>
          <dt id="AF_E164">AF_E164
          <dd><p>CCITT (ITU-T) E.164 recommendation</p>
          <dt id="AF_ECMA">AF_ECMA
          <dd><p>European Computer Manufacturers protocols</p>
          <dt id="AF_HYLINK">AF_HYLINK
          <dd><p>NSC Hyperchannel protocol</p>
          <dt id="AF_IB">AF_IB
          <dd><p>InfiniBand native addressing</p>
          <dt id="AF_IMPLINK">AF_IMPLINK
          <dd><p>ARPANET IMP protocol</p>
          <dt id="AF_INET">AF_INET
          <dd><p>IPv4 protocol</p>
          <dt id="AF_INET6">AF_INET6
          <dd><p>IPv6 protocol</p>
          <dt id="AF_IPX">AF_IPX
          <dd><p>IPX protocol</p>
          <dt id="AF_ISDN">AF_ISDN
          <dd><p>Integrated Services Digital Network</p>
          <dt id="AF_ISO">AF_ISO
          <dd><p>ISO Open Systems Interconnection protocols</p>
          <dt id="AF_KCM">AF_KCM
          <dd><p>KCM (kernel connection multiplexor) interface</p>
          <dt id="AF_KEY">AF_KEY
          <dd><p>Key management protocol, originally developed for usage with IPsec</p>
          <dt id="AF_LAT">AF_LAT
          <dd><p>Local Area Transport protocol</p>
          <dt id="AF_LINK">AF_LINK
          <dd><p>Link layer interface</p>
          <dt id="AF_LLC">AF_LLC
          <dd><p>Logical  link control (IEEE 802.2 LLC) protocol</p>
          <dt id="AF_LOCAL">AF_LOCAL
          <dd><p>Host-internal protocols</p>
          <dt id="AF_MAX">AF_MAX
          <dd><p>Maximum address family for this platform</p>
          <dt id="AF_MPLS">AF_MPLS
          <dd><p>Multiprotocol Label Switching</p>
          <dt id="AF_NATM">AF_NATM
          <dd><p>Native ATM access</p>
          <dt id="AF_NDRV">AF_NDRV
          <dd><p>Network driver raw access</p>
          <dt id="AF_NETBIOS">AF_NETBIOS
          <dd><p>NetBIOS</p>
          <dt id="AF_NETGRAPH">AF_NETGRAPH
          <dd><p>Netgraph sockets</p>
          <dt id="AF_NETLINK">AF_NETLINK
          <dd><p>Kernel user interface device</p>
          <dt id="AF_NS">AF_NS
          <dd><p>XEROX NS protocols</p>
          <dt id="AF_OSI">AF_OSI
          <dd><p>ISO Open Systems Interconnection protocols</p>
          <dt id="AF_PACKET">AF_PACKET
          <dd><p>Direct link-layer access</p>
          <dt id="AF_PPP">AF_PPP
          <dd><p>Point-to-Point Protocol</p>
          <dt id="AF_PPPOX">AF_PPPOX
          <dd><p>Generic PPP transport layer, for setting up L2 tunnels (L2TP and PPPoE)</p>
          <dt id="AF_PUP">AF_PUP
          <dd><p>PARC Universal Packet protocol</p>
          <dt id="AF_RDS">AF_RDS
          <dd><p>Reliable Datagram Sockets (RDS) protocol</p>
          <dt id="AF_ROUTE">AF_ROUTE
          <dd><p>Internal routing protocol</p>
          <dt id="AF_SIP">AF_SIP
          <dd><p>Simple Internet Protocol</p>
          <dt id="AF_SNA">AF_SNA
          <dd><p>IBM SNA protocol</p>
          <dt id="AF_SYSTEM">AF_SYSTEM
          <dd>
          <dt id="AF_TIPC">AF_TIPC
          <dd><p>TIPC, “cluster domain sockets” protocol</p>
          <dt id="AF_UNIX">AF_UNIX
          <dd><p>UNIX sockets</p>
          <dt id="AF_UNSPEC">AF_UNSPEC
          <dd><p>Unspecified protocol, any supported address family</p>
          <dt id="AF_VSOCK">AF_VSOCK
          <dd><p>VSOCK (originally “VMWare VSockets”) protocol for hypervisor-guest communication</p>
          <dt id="AF_XDP">AF_XDP
          <dd><p>XDP (express data path) interface</p>
          <dt id="AI_ADDRCONFIG">AI_ADDRCONFIG
          <dd><p>Accept only if any address is assigned</p>
          <dt id="AI_ALL">AI_ALL
          <dd><p>Allow all addresses</p>
          <dt id="AI_CANONNAME">AI_CANONNAME
          <dd><p>Fill in the canonical name</p>
          <dt id="AI_DEFAULT">AI_DEFAULT
          <dd><p>Default flags for getaddrinfo</p>
          <dt id="AI_MASK">AI_MASK
          <dd><p>Valid flag mask for getaddrinfo (not for application use)</p>
          <dt id="AI_NUMERICHOST">AI_NUMERICHOST
          <dd><p>Prevent host name resolution</p>
          <dt id="AI_NUMERICSERV">AI_NUMERICSERV
          <dd><p>Prevent service name resolution</p>
          <dt id="AI_PASSIVE">AI_PASSIVE
          <dd><p>Get address to use with bind()</p>
          <dt id="AI_V4MAPPED">AI_V4MAPPED
          <dd><p>Accept IPv4-mapped IPv6 addresses</p>
          <dt id="AI_V4MAPPED_CFG">AI_V4MAPPED_CFG
          <dd><p>Accept IPv4 mapped addresses if the kernel supports it</p>
          <dt id="EAI_ADDRFAMILY">EAI_ADDRFAMILY
          <dd><p>Address family for hostname not supported</p>
          <dt id="EAI_AGAIN">EAI_AGAIN
          <dd><p>Temporary failure in name resolution</p>
          <dt id="EAI_BADFLAGS">EAI_BADFLAGS
          <dd><p>Invalid flags</p>
          <dt id="EAI_BADHINTS">EAI_BADHINTS
          <dd><p>Invalid value for hints</p>
          <dt id="EAI_FAIL">EAI_FAIL
          <dd><p>Non-recoverable failure in name resolution</p>
          <dt id="EAI_FAMILY">EAI_FAMILY
          <dd><p>Address family not supported</p>
          <dt id="EAI_MAX">EAI_MAX
          <dd><p>Maximum error code from getaddrinfo</p>
          <dt id="EAI_MEMORY">EAI_MEMORY
          <dd><p>Memory allocation failure</p>
          <dt id="EAI_NODATA">EAI_NODATA
          <dd><p>No address associated with hostname</p>
          <dt id="EAI_NONAME">EAI_NONAME
          <dd><p>Hostname nor servname, or not known</p>
          <dt id="EAI_OVERFLOW">EAI_OVERFLOW
          <dd><p>Argument buffer overflow</p>
          <dt id="EAI_PROTOCOL">EAI_PROTOCOL
          <dd><p>Resolved protocol is unknown</p>
          <dt id="EAI_SERVICE">EAI_SERVICE
          <dd><p>Servname not supported for socket type</p>
          <dt id="EAI_SOCKTYPE">EAI_SOCKTYPE
          <dd><p><a href="Socket.html"><code>Socket</code></a> type not supported</p>
          <dt id="EAI_SYSTEM">EAI_SYSTEM
          <dd><p>System error returned in errno</p>
          <dt id="IFF_802_1Q_VLAN">IFF_802_1Q_VLAN
          <dd><p>802.1Q VLAN device</p>
          <dt id="IFF_ALLMULTI">IFF_ALLMULTI
          <dd><p>receive all multicast packets</p>
          <dt id="IFF_ALTPHYS">IFF_ALTPHYS
          <dd><p>use alternate physical connection</p>
          <dt id="IFF_AUTOMEDIA">IFF_AUTOMEDIA
          <dd><p>auto media select active</p>
          <dt id="IFF_BONDING">IFF_BONDING
          <dd><p>bonding master or slave</p>
          <dt id="IFF_BRIDGE_PORT">IFF_BRIDGE_PORT
          <dd><p>device used as bridge port</p>
          <dt id="IFF_BROADCAST">IFF_BROADCAST
          <dd><p>broadcast address valid</p>
          <dt id="IFF_CANTCHANGE">IFF_CANTCHANGE
          <dd><p>flags not changeable</p>
          <dt id="IFF_CANTCONFIG">IFF_CANTCONFIG
          <dd><p>unconfigurable using ioctl(2)</p>
          <dt id="IFF_DEBUG">IFF_DEBUG
          <dd><p>turn on debugging</p>
          <dt id="IFF_DISABLE_NETPOLL">IFF_DISABLE_NETPOLL
          <dd><p>disable netpoll at run-time</p>
          <dt id="IFF_DONT_BRIDGE">IFF_DONT_BRIDGE
          <dd><p>disallow bridging this ether dev</p>
          <dt id="IFF_DORMANT">IFF_DORMANT
          <dd><p>driver signals dormant</p>
          <dt id="IFF_DRV_OACTIVE">IFF_DRV_OACTIVE
          <dd><p>tx hardware queue is full</p>
          <dt id="IFF_DRV_RUNNING">IFF_DRV_RUNNING
          <dd><p>resources allocated</p>
          <dt id="IFF_DYING">IFF_DYING
          <dd><p>interface is winding down</p>
          <dt id="IFF_DYNAMIC">IFF_DYNAMIC
          <dd><p>dialup device with changing addresses</p>
          <dt id="IFF_EBRIDGE">IFF_EBRIDGE
          <dd><p>ethernet bridging device</p>
          <dt id="IFF_ECHO">IFF_ECHO
          <dd><p>echo sent packets</p>
          <dt id="IFF_ISATAP">IFF_ISATAP
          <dd><p>ISATAP interface (RFC4214)</p>
          <dt id="IFF_LINK0">IFF_LINK0
          <dd><p>per link layer defined bit 0</p>
          <dt id="IFF_LINK1">IFF_LINK1
          <dd><p>per link layer defined bit 1</p>
          <dt id="IFF_LINK2">IFF_LINK2
          <dd><p>per link layer defined bit 2</p>
          <dt id="IFF_LIVE_ADDR_CHANGE">IFF_LIVE_ADDR_CHANGE
          <dd><p>hardware address change when it’s running</p>
          <dt id="IFF_LOOPBACK">IFF_LOOPBACK
          <dd><p>loopback net</p>
          <dt id="IFF_LOWER_UP">IFF_LOWER_UP
          <dd><p>driver signals L1 up</p>
          <dt id="IFF_MACVLAN_PORT">IFF_MACVLAN_PORT
          <dd><p>device used as macvlan port</p>
          <dt id="IFF_MASTER">IFF_MASTER
          <dd><p>master of a load balancer</p>
          <dt id="IFF_MASTER_8023AD">IFF_MASTER_8023AD
          <dd><p>bonding master, 802.3ad.</p>
          <dt id="IFF_MASTER_ALB">IFF_MASTER_ALB
          <dd><p>bonding master, balance-alb.</p>
          <dt id="IFF_MASTER_ARPMON">IFF_MASTER_ARPMON
          <dd><p>bonding master, ARP mon in use</p>
          <dt id="IFF_MONITOR">IFF_MONITOR
          <dd><p>user-requested monitor mode</p>
          <dt id="IFF_MULTICAST">IFF_MULTICAST
          <dd><p>supports multicast</p>
          <dt id="IFF_NOARP">IFF_NOARP
          <dd><p>no address resolution protocol</p>
          <dt id="IFF_NOTRAILERS">IFF_NOTRAILERS
          <dd><p>avoid use of trailers</p>
          <dt id="IFF_OACTIVE">IFF_OACTIVE
          <dd><p>transmission in progress</p>
          <dt id="IFF_OVS_DATAPATH">IFF_OVS_DATAPATH
          <dd><p>device used as Open vSwitch datapath port</p>
          <dt id="IFF_POINTOPOINT">IFF_POINTOPOINT
          <dd><p>point-to-point link</p>
          <dt id="IFF_PORTSEL">IFF_PORTSEL
          <dd><p>can set media type</p>
          <dt id="IFF_PPROMISC">IFF_PPROMISC
          <dd><p>user-requested promisc mode</p>
          <dt id="IFF_PROMISC">IFF_PROMISC
          <dd><p>receive all packets</p>
          <dt id="IFF_RENAMING">IFF_RENAMING
          <dd><p>interface is being renamed</p>
          <dt id="IFF_ROUTE">IFF_ROUTE
          <dd><p>routing entry installed</p>
          <dt id="IFF_RUNNING">IFF_RUNNING
          <dd><p>resources allocated</p>
          <dt id="IFF_SIMPLEX">IFF_SIMPLEX
          <dd><p>can’t hear own transmissions</p>
          <dt id="IFF_SLAVE">IFF_SLAVE
          <dd><p>slave of a load balancer</p>
          <dt id="IFF_SLAVE_INACTIVE">IFF_SLAVE_INACTIVE
          <dd><p>bonding slave not the curr. active</p>
          <dt id="IFF_SLAVE_NEEDARP">IFF_SLAVE_NEEDARP
          <dd><p>need ARPs for validation</p>
          <dt id="IFF_SMART">IFF_SMART
          <dd><p>interface manages own routes</p>
          <dt id="IFF_STATICARP">IFF_STATICARP
          <dd><p>static ARP</p>
          <dt id="IFF_SUPP_NOFCS">IFF_SUPP_NOFCS
          <dd><p>sending custom FCS</p>
          <dt id="IFF_TEAM_PORT">IFF_TEAM_PORT
          <dd><p>used as team port</p>
          <dt id="IFF_TX_SKB_SHARING">IFF_TX_SKB_SHARING
          <dd><p>sharing skbs on transmit</p>
          <dt id="IFF_UNICAST_FLT">IFF_UNICAST_FLT
          <dd><p>unicast filtering</p>
          <dt id="IFF_UP">IFF_UP
          <dd><p>interface is up</p>
          <dt id="IFF_VOLATILE">IFF_VOLATILE
          <dd><p>volatile flags</p>
          <dt id="IFF_WAN_HDLC">IFF_WAN_HDLC
          <dd><p>WAN HDLC device</p>
          <dt id="IFF_XMIT_DST_RELEASE">IFF_XMIT_DST_RELEASE
          <dd><p>dev_hard_start_xmit() is allowed to release skb-&gt;dst</p>
          <dt id="IFNAMSIZ">IFNAMSIZ
          <dd><p>Maximum interface name size</p>
          <dt id="IF_NAMESIZE">IF_NAMESIZE
          <dd><p>Maximum interface name size</p>
          <dt id="INADDR_ALLHOSTS_GROUP">INADDR_ALLHOSTS_GROUP
          <dd><p>Multicast group for all systems on this subset</p>
          <dt id="INADDR_ANY">INADDR_ANY
          <dd><p>A socket bound to <a href="Socket.html#INADDR_ANY"><code>INADDR_ANY</code></a> receives packets from all interfaces and sends from the default IP address</p>
          <dt id="INADDR_BROADCAST">INADDR_BROADCAST
          <dd><p>The network broadcast address</p>
          <dt id="INADDR_LOOPBACK">INADDR_LOOPBACK
          <dd><p>The loopback address</p>
          <dt id="INADDR_MAX_LOCAL_GROUP">INADDR_MAX_LOCAL_GROUP
          <dd><p>The last local network multicast group</p>
          <dt id="INADDR_NONE">INADDR_NONE
          <dd><p>A bitmask for matching no valid IP address</p>
          <dt id="INADDR_UNSPEC_GROUP">INADDR_UNSPEC_GROUP
          <dd><p>The reserved multicast group</p>
          <dt id="INET6_ADDRSTRLEN">INET6_ADDRSTRLEN
          <dd><p>Maximum length of an IPv6 address string</p>
          <dt id="INET_ADDRSTRLEN">INET_ADDRSTRLEN
          <dd><p>Maximum length of an IPv4 address string</p>
          <dt id="IPPORT_RESERVED">IPPORT_RESERVED
          <dd><p>Default minimum address for bind or connect</p>
          <dt id="IPPORT_USERRESERVED">IPPORT_USERRESERVED
          <dd><p>Default maximum address for bind or connect</p>
          <dt id="IPPROTO_AH">IPPROTO_AH
          <dd><p>IP6 auth header</p>
          <dt id="IPPROTO_BIP">IPPROTO_BIP
          <dd>
          <dt id="IPPROTO_DSTOPTS">IPPROTO_DSTOPTS
          <dd><p>IP6 destination option</p>
          <dt id="IPPROTO_EGP">IPPROTO_EGP
          <dd><p>Exterior Gateway Protocol</p>
          <dt id="IPPROTO_EON">IPPROTO_EON
          <dd><p>ISO cnlp</p>
          <dt id="IPPROTO_ESP">IPPROTO_ESP
          <dd><p>IP6 Encapsulated Security Payload</p>
          <dt id="IPPROTO_FRAGMENT">IPPROTO_FRAGMENT
          <dd><p>IP6 fragmentation header</p>
          <dt id="IPPROTO_GGP">IPPROTO_GGP
          <dd><p>Gateway to Gateway Protocol</p>
          <dt id="IPPROTO_HELLO">IPPROTO_HELLO
          <dd><p>“hello” routing protocol</p>
          <dt id="IPPROTO_HOPOPTS">IPPROTO_HOPOPTS
          <dd><p>IP6 hop-by-hop options</p>
          <dt id="IPPROTO_ICMP">IPPROTO_ICMP
          <dd><p>Control message protocol</p>
          <dt id="IPPROTO_ICMPV6">IPPROTO_ICMPV6
          <dd><p>ICMP6</p>
          <dt id="IPPROTO_IDP">IPPROTO_IDP
          <dd><p>XNS IDP</p>
          <dt id="IPPROTO_IGMP">IPPROTO_IGMP
          <dd><p>Group Management Protocol</p>
          <dt id="IPPROTO_IP">IPPROTO_IP
          <dd><p>Dummy protocol for IP</p>
          <dt id="IPPROTO_IPV6">IPPROTO_IPV6
          <dd><p>IP6 header</p>
          <dt id="IPPROTO_MAX">IPPROTO_MAX
          <dd><p>Maximum IPPROTO constant</p>
          <dt id="IPPROTO_ND">IPPROTO_ND
          <dd><p>Sun net disk protocol</p>
          <dt id="IPPROTO_NONE">IPPROTO_NONE
          <dd><p>IP6 no next header</p>
          <dt id="IPPROTO_PUP">IPPROTO_PUP
          <dd><p>PARC Universal Packet protocol</p>
          <dt id="IPPROTO_RAW">IPPROTO_RAW
          <dd><p>Raw IP packet</p>
          <dt id="IPPROTO_ROUTING">IPPROTO_ROUTING
          <dd><p>IP6 routing header</p>
          <dt id="IPPROTO_TCP">IPPROTO_TCP
          <dd><p>TCP</p>
          <dt id="IPPROTO_TP">IPPROTO_TP
          <dd><p>ISO transport protocol class 4</p>
          <dt id="IPPROTO_UDP">IPPROTO_UDP
          <dd><p>UDP</p>
          <dt id="IPPROTO_XTP">IPPROTO_XTP
          <dd><p>Xpress Transport Protocol</p>
          <dt id="IPV6_CHECKSUM">IPV6_CHECKSUM
          <dd><p>Checksum offset for raw sockets</p>
          <dt id="IPV6_DONTFRAG">IPV6_DONTFRAG
          <dd><p>Don’t fragment packets</p>
          <dt id="IPV6_DSTOPTS">IPV6_DSTOPTS
          <dd><p>Destination option</p>
          <dt id="IPV6_HOPLIMIT">IPV6_HOPLIMIT
          <dd><p>Hop limit</p>
          <dt id="IPV6_HOPOPTS">IPV6_HOPOPTS
          <dd><p>Hop-by-hop option</p>
          <dt id="IPV6_JOIN_GROUP">IPV6_JOIN_GROUP
          <dd><p>Join a group membership</p>
          <dt id="IPV6_LEAVE_GROUP">IPV6_LEAVE_GROUP
          <dd><p>Leave a group membership</p>
          <dt id="IPV6_MULTICAST_HOPS">IPV6_MULTICAST_HOPS
          <dd><p>IP6 multicast hops</p>
          <dt id="IPV6_MULTICAST_IF">IPV6_MULTICAST_IF
          <dd><p>IP6 multicast interface</p>
          <dt id="IPV6_MULTICAST_LOOP">IPV6_MULTICAST_LOOP
          <dd><p>IP6 multicast loopback</p>
          <dt id="IPV6_NEXTHOP">IPV6_NEXTHOP
          <dd><p>Next hop address</p>
          <dt id="IPV6_PATHMTU">IPV6_PATHMTU
          <dd><p>Retrieve current path MTU</p>
          <dt id="IPV6_PKTINFO">IPV6_PKTINFO
          <dd><p>Receive packet information with datagram</p>
          <dt id="IPV6_RECVDSTOPTS">IPV6_RECVDSTOPTS
          <dd><p>Receive all IP6 options for response</p>
          <dt id="IPV6_RECVHOPLIMIT">IPV6_RECVHOPLIMIT
          <dd><p>Receive hop limit with datagram</p>
          <dt id="IPV6_RECVHOPOPTS">IPV6_RECVHOPOPTS
          <dd><p>Receive hop-by-hop options</p>
          <dt id="IPV6_RECVPATHMTU">IPV6_RECVPATHMTU
          <dd><p>Receive current path MTU with datagram</p>
          <dt id="IPV6_RECVPKTINFO">IPV6_RECVPKTINFO
          <dd><p>Receive destination IP address and incoming interface</p>
          <dt id="IPV6_RECVRTHDR">IPV6_RECVRTHDR
          <dd><p>Receive routing header</p>
          <dt id="IPV6_RECVTCLASS">IPV6_RECVTCLASS
          <dd><p>Receive traffic class</p>
          <dt id="IPV6_RTHDR">IPV6_RTHDR
          <dd><p>Allows removal of sticky routing headers</p>
          <dt id="IPV6_RTHDRDSTOPTS">IPV6_RTHDRDSTOPTS
          <dd><p>Allows removal of sticky destination options header</p>
          <dt id="IPV6_RTHDR_TYPE_0">IPV6_RTHDR_TYPE_0
          <dd><p>Routing header type 0</p>
          <dt id="IPV6_TCLASS">IPV6_TCLASS
          <dd><p>Specify the traffic class</p>
          <dt id="IPV6_UNICAST_HOPS">IPV6_UNICAST_HOPS
          <dd><p>IP6 unicast hops</p>
          <dt id="IPV6_USE_MIN_MTU">IPV6_USE_MIN_MTU
          <dd><p>Use the minimum MTU size</p>
          <dt id="IPV6_V6ONLY">IPV6_V6ONLY
          <dd><p>Only bind IPv6 with a wildcard bind</p>
          <dt id="IPX_TYPE">IPX_TYPE
          <dd>
          <dt id="IP_ADD_MEMBERSHIP">IP_ADD_MEMBERSHIP
          <dd><p>Add a multicast group membership</p>
          <dt id="IP_ADD_SOURCE_MEMBERSHIP">IP_ADD_SOURCE_MEMBERSHIP
          <dd><p>Add a multicast group membership</p>
          <dt id="IP_BLOCK_SOURCE">IP_BLOCK_SOURCE
          <dd><p>Block IPv4 multicast packets with a give source address</p>
          <dt id="IP_DEFAULT_MULTICAST_LOOP">IP_DEFAULT_MULTICAST_LOOP
          <dd><p>Default multicast loopback</p>
          <dt id="IP_DEFAULT_MULTICAST_TTL">IP_DEFAULT_MULTICAST_TTL
          <dd><p>Default multicast TTL</p>
          <dt id="IP_DONTFRAG">IP_DONTFRAG
          <dd><p>Don’t fragment packets</p>
          <dt id="IP_DROP_MEMBERSHIP">IP_DROP_MEMBERSHIP
          <dd><p>Drop a multicast group membership</p>
          <dt id="IP_DROP_SOURCE_MEMBERSHIP">IP_DROP_SOURCE_MEMBERSHIP
          <dd><p>Drop a multicast group membership</p>
          <dt id="IP_FREEBIND">IP_FREEBIND
          <dd><p>Allow binding to nonexistent IP addresses</p>
          <dt id="IP_HDRINCL">IP_HDRINCL
          <dd><p>Header is included with data</p>
          <dt id="IP_IPSEC_POLICY">IP_IPSEC_POLICY
          <dd><p>IPsec security policy</p>
          <dt id="IP_MAX_MEMBERSHIPS">IP_MAX_MEMBERSHIPS
          <dd><p>Maximum number multicast groups a socket can join</p>
          <dt id="IP_MINTTL">IP_MINTTL
          <dd><p>Minimum TTL allowed for received packets</p>
          <dt id="IP_MSFILTER">IP_MSFILTER
          <dd><p>Multicast source filtering</p>
          <dt id="IP_MTU">IP_MTU
          <dd><p>The Maximum Transmission Unit of the socket</p>
          <dt id="IP_MTU_DISCOVER">IP_MTU_DISCOVER
          <dd><p>Path MTU discovery</p>
          <dt id="IP_MULTICAST_IF">IP_MULTICAST_IF
          <dd><p>IP multicast interface</p>
          <dt id="IP_MULTICAST_LOOP">IP_MULTICAST_LOOP
          <dd><p>IP multicast loopback</p>
          <dt id="IP_MULTICAST_TTL">IP_MULTICAST_TTL
          <dd><p>IP multicast TTL</p>
          <dt id="IP_ONESBCAST">IP_ONESBCAST
          <dd><p>Force outgoing broadcast datagrams to have the undirected broadcast address</p>
          <dt id="IP_OPTIONS">IP_OPTIONS
          <dd><p>IP options to be included in packets</p>
          <dt id="IP_PASSSEC">IP_PASSSEC
          <dd><p>Retrieve security context with datagram</p>
          <dt id="IP_PKTINFO">IP_PKTINFO
          <dd><p>Receive packet information with datagrams</p>
          <dt id="IP_PKTOPTIONS">IP_PKTOPTIONS
          <dd><p>Receive packet options with datagrams</p>
          <dt id="IP_PMTUDISC_DO">IP_PMTUDISC_DO
          <dd><p>Always send DF frames</p>
          <dt id="IP_PMTUDISC_DONT">IP_PMTUDISC_DONT
          <dd><p>Never send DF frames</p>
          <dt id="IP_PMTUDISC_WANT">IP_PMTUDISC_WANT
          <dd><p>Use per-route hints</p>
          <dt id="IP_PORTRANGE">IP_PORTRANGE
          <dd><p>Set the port range for sockets with unspecified port numbers</p>
          <dt id="IP_RECVDSTADDR">IP_RECVDSTADDR
          <dd><p>Receive IP destination address with datagram</p>
          <dt id="IP_RECVERR">IP_RECVERR
          <dd><p>Enable extended reliable error message passing</p>
          <dt id="IP_RECVIF">IP_RECVIF
          <dd><p>Receive interface information with datagrams</p>
          <dt id="IP_RECVOPTS">IP_RECVOPTS
          <dd><p>Receive all IP options with datagram</p>
          <dt id="IP_RECVRETOPTS">IP_RECVRETOPTS
          <dd><p>Receive all IP options for response</p>
          <dt id="IP_RECVSLLA">IP_RECVSLLA
          <dd><p>Receive link-layer address with datagrams</p>
          <dt id="IP_RECVTOS">IP_RECVTOS
          <dd><p>Receive TOS with incoming packets</p>
          <dt id="IP_RECVTTL">IP_RECVTTL
          <dd><p>Receive IP TTL with datagrams</p>
          <dt id="IP_RETOPTS">IP_RETOPTS
          <dd><p>IP options to be included in datagrams</p>
          <dt id="IP_ROUTER_ALERT">IP_ROUTER_ALERT
          <dd><p>Notify transit routers to more closely examine the contents of an IP packet</p>
          <dt id="IP_SENDSRCADDR">IP_SENDSRCADDR
          <dd><p>Source address for outgoing UDP datagrams</p>
          <dt id="IP_TOS">IP_TOS
          <dd><p>IP type-of-service</p>
          <dt id="IP_TRANSPARENT">IP_TRANSPARENT
          <dd><p>Transparent proxy</p>
          <dt id="IP_TTL">IP_TTL
          <dd><p>IP time-to-live</p>
          <dt id="IP_UNBLOCK_SOURCE">IP_UNBLOCK_SOURCE
          <dd><p>Unblock IPv4 multicast packets with a give source address</p>
          <dt id="IP_XFRM_POLICY">IP_XFRM_POLICY
          <dd>
          <dt id="LOCAL_CONNWAIT">LOCAL_CONNWAIT
          <dd><p>Connect blocks until accepted</p>
          <dt id="LOCAL_CREDS">LOCAL_CREDS
          <dd><p>Pass credentials to receiver</p>
          <dt id="LOCAL_PEERCRED">LOCAL_PEERCRED
          <dd><p>Retrieve peer credentials</p>
          <dt id="MCAST_BLOCK_SOURCE">MCAST_BLOCK_SOURCE
          <dd><p>Block multicast packets from this source</p>
          <dt id="MCAST_EXCLUDE">MCAST_EXCLUDE
          <dd><p>Exclusive multicast source filter</p>
          <dt id="MCAST_INCLUDE">MCAST_INCLUDE
          <dd><p>Inclusive multicast source filter</p>
          <dt id="MCAST_JOIN_GROUP">MCAST_JOIN_GROUP
          <dd><p>Join a multicast group</p>
          <dt id="MCAST_JOIN_SOURCE_GROUP">MCAST_JOIN_SOURCE_GROUP
          <dd><p>Join a multicast source group</p>
          <dt id="MCAST_LEAVE_GROUP">MCAST_LEAVE_GROUP
          <dd><p>Leave a multicast group</p>
          <dt id="MCAST_LEAVE_SOURCE_GROUP">MCAST_LEAVE_SOURCE_GROUP
          <dd><p>Leave a multicast source group</p>
          <dt id="MCAST_MSFILTER">MCAST_MSFILTER
          <dd><p>Multicast source filtering</p>
          <dt id="MCAST_UNBLOCK_SOURCE">MCAST_UNBLOCK_SOURCE
          <dd><p>Unblock multicast packets from this source</p>
          <dt id="MSG_COMPAT">MSG_COMPAT
          <dd><p>End of record</p>
          <dt id="MSG_CONFIRM">MSG_CONFIRM
          <dd><p>Confirm path validity</p>
          <dt id="MSG_CTRUNC">MSG_CTRUNC
          <dd><p>Control data lost before delivery</p>
          <dt id="MSG_DONTROUTE">MSG_DONTROUTE
          <dd><p>Send without using the routing tables</p>
          <dt id="MSG_DONTWAIT">MSG_DONTWAIT
          <dd><p>This message should be non-blocking</p>
          <dt id="MSG_EOF">MSG_EOF
          <dd><p>Data completes connection</p>
          <dt id="MSG_EOR">MSG_EOR
          <dd><p>Data completes record</p>
          <dt id="MSG_ERRQUEUE">MSG_ERRQUEUE
          <dd><p>Fetch message from error queue</p>
          <dt id="MSG_FASTOPEN">MSG_FASTOPEN
          <dd><p>Reduce step of the handshake process</p>
          <dt id="MSG_FIN">MSG_FIN
          <dd>
          <dt id="MSG_FLUSH">MSG_FLUSH
          <dd><p>Start of a hold sequence.  Dumps to so_temp</p>
          <dt id="MSG_HAVEMORE">MSG_HAVEMORE
          <dd><p>Data ready to be read</p>
          <dt id="MSG_HOLD">MSG_HOLD
          <dd><p>Hold fragment in so_temp</p>
          <dt id="MSG_MORE">MSG_MORE
          <dd><p>Sender will send more</p>
          <dt id="MSG_NOSIGNAL">MSG_NOSIGNAL
          <dd><p>Do not generate SIGPIPE</p>
          <dt id="MSG_OOB">MSG_OOB
          <dd><p>Process out-of-band data</p>
          <dt id="MSG_PEEK">MSG_PEEK
          <dd><p>Peek at incoming message</p>
          <dt id="MSG_PROXY">MSG_PROXY
          <dd><p>Wait for full request</p>
          <dt id="MSG_RCVMORE">MSG_RCVMORE
          <dd><p>Data remains in the current packet</p>
          <dt id="MSG_RST">MSG_RST
          <dd>
          <dt id="MSG_SEND">MSG_SEND
          <dd><p>Send the packet in so_temp</p>
          <dt id="MSG_SYN">MSG_SYN
          <dd>
          <dt id="MSG_TRUNC">MSG_TRUNC
          <dd><p>Data discarded before delivery</p>
          <dt id="MSG_WAITALL">MSG_WAITALL
          <dd><p>Wait for full request or error</p>
          <dt id="NI_DGRAM">NI_DGRAM
          <dd><p>The service specified is a datagram service (looks up UDP ports)</p>
          <dt id="NI_MAXHOST">NI_MAXHOST
          <dd><p>Maximum length of a hostname</p>
          <dt id="NI_MAXSERV">NI_MAXSERV
          <dd><p>Maximum length of a service name</p>
          <dt id="NI_NAMEREQD">NI_NAMEREQD
          <dd><p>A name is required</p>
          <dt id="NI_NOFQDN">NI_NOFQDN
          <dd><p>An FQDN is not required for local hosts, return only the local part</p>
          <dt id="NI_NUMERICHOST">NI_NUMERICHOST
          <dd><p>Return a numeric address</p>
          <dt id="NI_NUMERICSERV">NI_NUMERICSERV
          <dd><p>Return the service name as a digit string</p>
          <dt id="PF_ALG">PF_ALG
          <dd><p>Interface to kernel crypto API</p>
          <dt id="PF_APPLETALK">PF_APPLETALK
          <dd><p>AppleTalk protocol</p>
          <dt id="PF_ATM">PF_ATM
          <dd><p>Asynchronous Transfer Mode</p>
          <dt id="PF_AX25">PF_AX25
          <dd><p>AX.25 protocol</p>
          <dt id="PF_BLUETOOTH">PF_BLUETOOTH
          <dd><p>Bluetooth low-level socket protocol</p>
          <dt id="PF_CAN">PF_CAN
          <dd><p>Controller Area Network automotive bus protocol</p>
          <dt id="PF_CCITT">PF_CCITT
          <dd><p>CCITT (now ITU-T) protocols</p>
          <dt id="PF_CHAOS">PF_CHAOS
          <dd><p>MIT CHAOS protocols</p>
          <dt id="PF_CNT">PF_CNT
          <dd><p>Computer Network Technology</p>
          <dt id="PF_COIP">PF_COIP
          <dd><p>Connection-oriented IP</p>
          <dt id="PF_DATAKIT">PF_DATAKIT
          <dd><p>Datakit protocol</p>
          <dt id="PF_DEC">PF_DEC
          <dd><p>DECnet protocol</p>
          <dt id="PF_DECnet">PF_DECnet
          <dd><p>DECnet protocol</p>
          <dt id="PF_DLI">PF_DLI
          <dd><p>DEC Direct Data Link Interface protocol</p>
          <dt id="PF_ECMA">PF_ECMA
          <dd><p>European Computer Manufacturers protocols</p>
          <dt id="PF_HYLINK">PF_HYLINK
          <dd><p>NSC Hyperchannel protocol</p>
          <dt id="PF_IB">PF_IB
          <dd><p>InfiniBand native addressing</p>
          <dt id="PF_IMPLINK">PF_IMPLINK
          <dd><p>ARPANET IMP protocol</p>
          <dt id="PF_INET">PF_INET
          <dd><p>IPv4 protocol</p>
          <dt id="PF_INET6">PF_INET6
          <dd><p>IPv6 protocol</p>
          <dt id="PF_IPX">PF_IPX
          <dd><p>IPX protocol</p>
          <dt id="PF_ISDN">PF_ISDN
          <dd><p>Integrated Services Digital Network</p>
          <dt id="PF_ISO">PF_ISO
          <dd><p>ISO Open Systems Interconnection protocols</p>
          <dt id="PF_KCM">PF_KCM
          <dd><p>KCM (kernel connection multiplexor) interface</p>
          <dt id="PF_KEY">PF_KEY
          <dd><p>Key management protocol, originally developed for usage with IPsec</p>
          <dt id="PF_LAT">PF_LAT
          <dd><p>Local Area Transport protocol</p>
          <dt id="PF_LINK">PF_LINK
          <dd><p>Link layer interface</p>
          <dt id="PF_LLC">PF_LLC
          <dd><p>Logical  link control (IEEE 802.2 LLC) protocol</p>
          <dt id="PF_LOCAL">PF_LOCAL
          <dd><p>Host-internal protocols</p>
          <dt id="PF_MAX">PF_MAX
          <dd><p>Maximum address family for this platform</p>
          <dt id="PF_MPLS">PF_MPLS
          <dd><p>Multiprotocol Label Switching</p>
          <dt id="PF_NATM">PF_NATM
          <dd><p>Native ATM access</p>
          <dt id="PF_NDRV">PF_NDRV
          <dd><p>Network driver raw access</p>
          <dt id="PF_NETBIOS">PF_NETBIOS
          <dd><p>NetBIOS</p>
          <dt id="PF_NETGRAPH">PF_NETGRAPH
          <dd><p>Netgraph sockets</p>
          <dt id="PF_NETLINK">PF_NETLINK
          <dd><p>Kernel user interface device</p>
          <dt id="PF_NS">PF_NS
          <dd><p>XEROX NS protocols</p>
          <dt id="PF_OSI">PF_OSI
          <dd><p>ISO Open Systems Interconnection protocols</p>
          <dt id="PF_PACKET">PF_PACKET
          <dd><p>Direct link-layer access</p>
          <dt id="PF_PIP">PF_PIP
          <dd>
          <dt id="PF_PPP">PF_PPP
          <dd><p>Point-to-Point Protocol</p>
          <dt id="PF_PPPOX">PF_PPPOX
          <dd><p>Generic PPP transport layer, for setting up L2 tunnels (L2TP and PPPoE)</p>
          <dt id="PF_PUP">PF_PUP
          <dd><p>PARC Universal Packet protocol</p>
          <dt id="PF_RDS">PF_RDS
          <dd><p>Reliable Datagram Sockets (RDS) protocol</p>
          <dt id="PF_ROUTE">PF_ROUTE
          <dd><p>Internal routing protocol</p>
          <dt id="PF_RTIP">PF_RTIP
          <dd>
          <dt id="PF_SIP">PF_SIP
          <dd><p>Simple Internet Protocol</p>
          <dt id="PF_SNA">PF_SNA
          <dd><p>IBM SNA protocol</p>
          <dt id="PF_SYSTEM">PF_SYSTEM
          <dd>
          <dt id="PF_TIPC">PF_TIPC
          <dd><p>TIPC, “cluster domain sockets” protocol</p>
          <dt id="PF_UNIX">PF_UNIX
          <dd><p>UNIX sockets</p>
          <dt id="PF_UNSPEC">PF_UNSPEC
          <dd><p>Unspecified protocol, any supported address family</p>
          <dt id="PF_VSOCK">PF_VSOCK
          <dd><p>VSOCK (originally “VMWare VSockets”) protocol for hypervisor-guest communication</p>
          <dt id="PF_XDP">PF_XDP
          <dd><p>XDP (express data path) interface</p>
          <dt id="PF_XTP">PF_XTP
          <dd><p>eXpress Transfer Protocol</p>
          <dt id="SCM_BINTIME">SCM_BINTIME
          <dd><p>Timestamp (bintime)</p>
          <dt id="SCM_CREDENTIALS">SCM_CREDENTIALS
          <dd><p>The sender’s credentials</p>
          <dt id="SCM_CREDS">SCM_CREDS
          <dd><p>Process credentials</p>
          <dt id="SCM_RIGHTS">SCM_RIGHTS
          <dd><p>Access rights</p>
          <dt id="SCM_TIMESTAMP">SCM_TIMESTAMP
          <dd><p>Timestamp (timeval)</p>
          <dt id="SCM_TIMESTAMPING">SCM_TIMESTAMPING
          <dd><p>Timestamp (timespec list) (Linux 2.6.30)</p>
          <dt id="SCM_TIMESTAMPNS">SCM_TIMESTAMPNS
          <dd><p>Timespec (timespec)</p>
          <dt id="SCM_UCRED">SCM_UCRED
          <dd><p>User credentials</p>
          <dt id="SCM_WIFI_STATUS">SCM_WIFI_STATUS
          <dd><p>Wifi status (Linux 3.3)</p>
          <dt id="SHUT_RD">SHUT_RD
          <dd><p>Shut down the reading side of the socket</p>
          <dt id="SHUT_RDWR">SHUT_RDWR
          <dd><p>Shut down the both sides of the socket</p>
          <dt id="SHUT_WR">SHUT_WR
          <dd><p>Shut down the writing side of the socket</p>
          <dt id="SOCK_CLOEXEC">SOCK_CLOEXEC
          <dd><p>Set the close-on-exec (FD_CLOEXEC) flag on the new file  descriptor.</p>
          <dt id="SOCK_DGRAM">SOCK_DGRAM
          <dd><p>A datagram socket provides connectionless, unreliable messaging</p>
          <dt id="SOCK_NONBLOCK">SOCK_NONBLOCK
          <dd><p>Set the O_NONBLOCK file status flag on the open file description (see open(2)) referred to by the new file descriptor.</p>
          <dt id="SOCK_PACKET">SOCK_PACKET
          <dd><p>Device-level packet access</p>
          <dt id="SOCK_RAW">SOCK_RAW
          <dd><p>A raw socket provides low-level access for direct access or implementing network protocols</p>
          <dt id="SOCK_RDM">SOCK_RDM
          <dd><p>A reliable datagram socket provides reliable delivery of messages</p>
          <dt id="SOCK_SEQPACKET">SOCK_SEQPACKET
          <dd><p>A sequential packet socket provides sequenced, reliable two-way connection for datagrams</p>
          <dt id="SOCK_STREAM">SOCK_STREAM
          <dd><p>A stream socket provides a sequenced, reliable two-way connection for a byte stream</p>
          <dt id="SOL_ATALK">SOL_ATALK
          <dd><p>AppleTalk socket options</p>
          <dt id="SOL_AX25">SOL_AX25
          <dd><p>AX.25 socket options</p>
          <dt id="SOL_IP">SOL_IP
          <dd><p>IP socket options</p>
          <dt id="SOL_IPX">SOL_IPX
          <dd><p>IPX socket options</p>
          <dt id="SOL_SOCKET">SOL_SOCKET
          <dd><p>Socket-level options</p>
          <dt id="SOL_TCP">SOL_TCP
          <dd><p>TCP socket options</p>
          <dt id="SOL_UDP">SOL_UDP
          <dd><p>UDP socket options</p>
          <dt id="SOMAXCONN">SOMAXCONN
          <dd><p>Maximum connection requests that may be queued for a socket</p>
          <dt id="SOPRI_BACKGROUND">SOPRI_BACKGROUND
          <dd><p>Background socket priority</p>
          <dt id="SOPRI_INTERACTIVE">SOPRI_INTERACTIVE
          <dd><p>Interactive socket priority</p>
          <dt id="SOPRI_NORMAL">SOPRI_NORMAL
          <dd><p>Normal socket priority</p>
          <dt id="SO_ACCEPTCONN">SO_ACCEPTCONN
          <dd><p><a href="Socket.html"><code>Socket</code></a> has had listen() called on it</p>
          <dt id="SO_ACCEPTFILTER">SO_ACCEPTFILTER
          <dd><p>There is an accept filter</p>
          <dt id="SO_ALLZONES">SO_ALLZONES
          <dd><p>Bypass zone boundaries</p>
          <dt id="SO_ATTACH_FILTER">SO_ATTACH_FILTER
          <dd><p>Attach an accept filter</p>
          <dt id="SO_BINDTODEVICE">SO_BINDTODEVICE
          <dd><p>Only send packets from the given interface</p>
          <dt id="SO_BINTIME">SO_BINTIME
          <dd><p>Receive timestamp with datagrams (bintime)</p>
          <dt id="SO_BPF_EXTENSIONS">SO_BPF_EXTENSIONS
          <dd><p>Query supported BPF extensions (Linux 3.14)</p>
          <dt id="SO_BROADCAST">SO_BROADCAST
          <dd><p>Permit sending of broadcast messages</p>
          <dt id="SO_BUSY_POLL">SO_BUSY_POLL
          <dd><p>Set the threshold in microseconds for low latency polling (Linux 3.11)</p>
          <dt id="SO_DEBUG">SO_DEBUG
          <dd><p>Debug info recording</p>
          <dt id="SO_DETACH_FILTER">SO_DETACH_FILTER
          <dd><p>Detach an accept filter</p>
          <dt id="SO_DOMAIN">SO_DOMAIN
          <dd><p>Domain given for socket() (Linux 2.6.32)</p>
          <dt id="SO_DONTROUTE">SO_DONTROUTE
          <dd><p>Use interface addresses</p>
          <dt id="SO_DONTTRUNC">SO_DONTTRUNC
          <dd><p>Retain unread data</p>
          <dt id="SO_ERROR">SO_ERROR
          <dd><p>Get and clear the error status</p>
          <dt id="SO_GET_FILTER">SO_GET_FILTER
          <dd><p>Obtain filter set by <a href="Socket.html#SO_ATTACH_FILTER"><code>SO_ATTACH_FILTER</code></a> (Linux 3.8)</p>
          <dt id="SO_INCOMING_CPU">SO_INCOMING_CPU
          <dd><p>Receive the cpu attached to the socket (Linux 3.19)</p>
          <dt id="SO_INCOMING_NAPI_ID">SO_INCOMING_NAPI_ID
          <dd><p>Receive the napi ID attached to a RX queue (Linux 4.12)</p>
          <dt id="SO_KEEPALIVE">SO_KEEPALIVE
          <dd><p>Keep connections alive</p>
          <dt id="SO_LINGER">SO_LINGER
          <dd><p>Linger on close if data is present</p>
          <dt id="SO_LOCK_FILTER">SO_LOCK_FILTER
          <dd><p>Lock the filter attached to a socket (Linux 3.9)</p>
          <dt id="SO_MAC_EXEMPT">SO_MAC_EXEMPT
          <dd><p>Mandatory Access Control exemption for unlabeled peers</p>
          <dt id="SO_MARK">SO_MARK
          <dd><p>Set the mark for mark-based routing (Linux 2.6.25)</p>
          <dt id="SO_MAX_PACING_RATE">SO_MAX_PACING_RATE
          <dd><p>Cap the rate computed by transport layer. [bytes per second] (Linux 3.13)</p>
          <dt id="SO_NKE">SO_NKE
          <dd><p>Install socket-level Network Kernel Extension</p>
          <dt id="SO_NOFCS">SO_NOFCS
          <dd><p>Set netns of a socket (Linux 3.4)</p>
          <dt id="SO_NOSIGPIPE">SO_NOSIGPIPE
          <dd><p>Don’t SIGPIPE on EPIPE</p>
          <dt id="SO_NO_CHECK">SO_NO_CHECK
          <dd><p>Disable checksums</p>
          <dt id="SO_NREAD">SO_NREAD
          <dd><p>Get first packet byte count</p>
          <dt id="SO_OOBINLINE">SO_OOBINLINE
          <dd><p>Leave received out-of-band data in-line</p>
          <dt id="SO_PASSCRED">SO_PASSCRED
          <dd><p>Receive <a href="Socket.html#SCM_CREDENTIALS"><code>SCM_CREDENTIALS</code></a> messages</p>
          <dt id="SO_PASSSEC">SO_PASSSEC
          <dd><p>Toggle security context passing (Linux 2.6.18)</p>
          <dt id="SO_PEEK_OFF">SO_PEEK_OFF
          <dd><p>Set the peek offset (Linux 3.4)</p>
          <dt id="SO_PEERCRED">SO_PEERCRED
          <dd><p>The credentials of the foreign process connected to this socket</p>
          <dt id="SO_PEERNAME">SO_PEERNAME
          <dd><p>Name of the connecting user</p>
          <dt id="SO_PEERSEC">SO_PEERSEC
          <dd><p>Obtain the security credentials (Linux 2.6.2)</p>
          <dt id="SO_PRIORITY">SO_PRIORITY
          <dd><p>The protocol-defined priority for all packets on this socket</p>
          <dt id="SO_PROTOCOL">SO_PROTOCOL
          <dd><p>Protocol given for socket() (Linux 2.6.32)</p>
          <dt id="SO_RCVBUF">SO_RCVBUF
          <dd><p>Receive buffer size</p>
          <dt id="SO_RCVBUFFORCE">SO_RCVBUFFORCE
          <dd><p>Receive buffer size without rmem_max limit (Linux 2.6.14)</p>
          <dt id="SO_RCVLOWAT">SO_RCVLOWAT
          <dd><p>Receive low-water mark</p>
          <dt id="SO_RCVTIMEO">SO_RCVTIMEO
          <dd><p>Receive timeout</p>
          <dt id="SO_RECVUCRED">SO_RECVUCRED
          <dd><p>Receive user credentials with datagram</p>
          <dt id="SO_REUSEADDR">SO_REUSEADDR
          <dd><p>Allow local address reuse</p>
          <dt id="SO_REUSEPORT">SO_REUSEPORT
          <dd><p>Allow local address and port reuse</p>
          <dt id="SO_RTABLE">SO_RTABLE
          <dd><p>Set the routing table for this socket (OpenBSD)</p>
          <dt id="SO_RXQ_OVFL">SO_RXQ_OVFL
          <dd><p>Toggle cmsg for number of packets dropped (Linux 2.6.33)</p>
          <dt id="SO_SECURITY_AUTHENTICATION">SO_SECURITY_AUTHENTICATION
          <dd>
          <dt id="SO_SECURITY_ENCRYPTION_NETWORK">SO_SECURITY_ENCRYPTION_NETWORK
          <dd>
          <dt id="SO_SECURITY_ENCRYPTION_TRANSPORT">SO_SECURITY_ENCRYPTION_TRANSPORT
          <dd>
          <dt id="SO_SELECT_ERR_QUEUE">SO_SELECT_ERR_QUEUE
          <dd><p>Make select() detect socket error queue with errorfds (Linux 3.10)</p>
          <dt id="SO_SETFIB">SO_SETFIB
          <dd><p>Set the associated routing table for the socket (FreeBSD)</p>
          <dt id="SO_SNDBUF">SO_SNDBUF
          <dd><p>Send buffer size</p>
          <dt id="SO_SNDBUFFORCE">SO_SNDBUFFORCE
          <dd><p>Send buffer size without wmem_max limit (Linux 2.6.14)</p>
          <dt id="SO_SNDLOWAT">SO_SNDLOWAT
          <dd><p>Send low-water mark</p>
          <dt id="SO_SNDTIMEO">SO_SNDTIMEO
          <dd><p>Send timeout</p>
          <dt id="SO_TIMESTAMP">SO_TIMESTAMP
          <dd><p>Receive timestamp with datagrams (timeval)</p>
          <dt id="SO_TIMESTAMPING">SO_TIMESTAMPING
          <dd><p>Time stamping of incoming and outgoing packets (Linux 2.6.30)</p>
          <dt id="SO_TIMESTAMPNS">SO_TIMESTAMPNS
          <dd><p>Receive nanosecond timestamp with datagrams (timespec)</p>
          <dt id="SO_TYPE">SO_TYPE
          <dd><p>Get the socket type</p>
          <dt id="SO_USELOOPBACK">SO_USELOOPBACK
          <dd><p>Bypass hardware when possible</p>
          <dt id="SO_USER_COOKIE">SO_USER_COOKIE
          <dd><p>Setting an identifier for ipfw purpose mainly</p>
          <dt id="SO_WANTMORE">SO_WANTMORE
          <dd><p>Give a hint when more data is ready</p>
          <dt id="SO_WANTOOBFLAG">SO_WANTOOBFLAG
          <dd><p>OOB data is wanted in MSG_FLAG on receive</p>
          <dt id="SO_WIFI_STATUS">SO_WIFI_STATUS
          <dd><p>Toggle cmsg for wifi status (Linux 3.3)</p>
          <dt id="TCP_CONGESTION">TCP_CONGESTION
          <dd><p>TCP congestion control algorithm (Linux 2.6.13, glibc 2.6)</p>
          <dt id="TCP_CONNECTION_INFO">TCP_CONNECTION_INFO
          <dd><p>Retrieve information about this socket (macOS)</p>
          <dt id="TCP_COOKIE_TRANSACTIONS">TCP_COOKIE_TRANSACTIONS
          <dd><p>TCP Cookie Transactions (Linux 2.6.33, glibc 2.18)</p>
          <dt id="TCP_CORK">TCP_CORK
          <dd><p>Don’t send partial frames (Linux 2.2, glibc 2.2)</p>
          <dt id="TCP_DEFER_ACCEPT">TCP_DEFER_ACCEPT
          <dd><p>Don’t notify a listening socket until data is ready (Linux 2.4, glibc 2.2)</p>
          <dt id="TCP_FASTOPEN">TCP_FASTOPEN
          <dd><p>Reduce step of the handshake process (Linux 3.7, glibc 2.18)</p>
          <dt id="TCP_INFO">TCP_INFO
          <dd><p>Retrieve information about this socket (Linux 2.4, glibc 2.2)</p>
          <dt id="TCP_KEEPALIVE">TCP_KEEPALIVE
          <dd><p>Idle time before keepalive probes are sent (macOS)</p>
          <dt id="TCP_KEEPCNT">TCP_KEEPCNT
          <dd><p>Maximum number of keepalive probes allowed before dropping a connection (Linux 2.4, glibc 2.2)</p>
          <dt id="TCP_KEEPIDLE">TCP_KEEPIDLE
          <dd><p>Idle time before keepalive probes are sent (Linux 2.4, glibc 2.2)</p>
          <dt id="TCP_KEEPINTVL">TCP_KEEPINTVL
          <dd><p>Time between keepalive probes (Linux 2.4, glibc 2.2)</p>
          <dt id="TCP_LINGER2">TCP_LINGER2
          <dd><p>Lifetime of orphaned FIN_WAIT2 sockets (Linux 2.4, glibc 2.2)</p>
          <dt id="TCP_MAXSEG">TCP_MAXSEG
          <dd><p>Set maximum segment size</p>
          <dt id="TCP_MD5SIG">TCP_MD5SIG
          <dd><p>Use MD5 digests (RFC2385, Linux 2.6.20, glibc 2.7)</p>
          <dt id="TCP_NODELAY">TCP_NODELAY
          <dd><p>Don’t delay sending to coalesce packets</p>
          <dt id="TCP_NOOPT">TCP_NOOPT
          <dd><p>Don’t use TCP options</p>
          <dt id="TCP_NOPUSH">TCP_NOPUSH
          <dd><p>Don’t push the last block of write</p>
          <dt id="TCP_QUEUE_SEQ">TCP_QUEUE_SEQ
          <dd><p>Sequence of a queue for repair mode (Linux 3.5, glibc 2.18)</p>
          <dt id="TCP_QUICKACK">TCP_QUICKACK
          <dd><p>Enable quickack mode (Linux 2.4.4, glibc 2.3)</p>
          <dt id="TCP_REPAIR">TCP_REPAIR
          <dd><p>Repair mode (Linux 3.5, glibc 2.18)</p>
          <dt id="TCP_REPAIR_OPTIONS">TCP_REPAIR_OPTIONS
          <dd><p>Options for repair mode (Linux 3.5, glibc 2.18)</p>
          <dt id="TCP_REPAIR_QUEUE">TCP_REPAIR_QUEUE
          <dd><p>Queue for repair mode (Linux 3.5, glibc 2.18)</p>
          <dt id="TCP_SYNCNT">TCP_SYNCNT
          <dd><p>Number of SYN retransmits before a connection is dropped (Linux 2.4, glibc 2.2)</p>
          <dt id="TCP_THIN_DUPACK">TCP_THIN_DUPACK
          <dd><p>Duplicated acknowledgments handling for thin-streams (Linux 2.6.34, glibc 2.18)</p>
          <dt id="TCP_THIN_LINEAR_TIMEOUTS">TCP_THIN_LINEAR_TIMEOUTS
          <dd><p>Linear timeouts for thin-streams (Linux 2.6.34, glibc 2.18)</p>
          <dt id="TCP_TIMESTAMP">TCP_TIMESTAMP
          <dd><p>TCP timestamp (Linux 3.9, glibc 2.18)</p>
          <dt id="TCP_USER_TIMEOUT">TCP_USER_TIMEOUT
          <dd><p>Max timeout before a TCP connection is aborted (Linux 2.6.37, glibc 2.18)</p>
          <dt id="TCP_WINDOW_CLAMP">TCP_WINDOW_CLAMP
          <dd><p>Clamp the size of the advertised window (Linux 2.4, glibc 2.2)</p>
          <dt id="UDP_CORK">UDP_CORK
          <dd><p>Don’t send partial frames (Linux 2.5.44, glibc 2.11)</p>
        </dl>
        </section>



                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-accept_loop" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">accept_loop</span><span
                                class="method-args">(*sockets) { |socket, client_addrinfo| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>yield socket and client address for each a connection accepted via given sockets.</p>

<p>The arguments are a list of sockets. The individual argument should be a socket or an array of sockets.</p>

<p>This method yields the block sequentially. It means that the next connection is not accepted until the block returns. So concurrent mechanism, thread for example, should be used to service multiple clients at a time.</p>

                              <div class="method-source-code" id="accept_loop-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 803</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">accept_loop</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">sockets</span>) <span class="ruby-comment"># :yield: socket, client_addrinfo</span>
  <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">flatten!</span>(<span class="ruby-value">1</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">empty?</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;no sockets&quot;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">loop</span> {
    <span class="ruby-identifier">readable</span>, <span class="ruby-identifier">_</span>, <span class="ruby-identifier">_</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>(<span class="ruby-identifier">sockets</span>)
    <span class="ruby-identifier">readable</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">sock</span>, <span class="ruby-identifier">addr</span> = <span class="ruby-identifier">r</span>.<span class="ruby-identifier">accept_nonblock</span>(<span class="ruby-value">exception:</span> <span class="ruby-keyword">false</span>)
      <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">sock</span> <span class="ruby-operator">==</span> <span class="ruby-value">:wait_readable</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">sock</span>, <span class="ruby-identifier">addr</span>
    }
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-getaddrinfo" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          getaddrinfo(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Obtains address information for <em>nodename</em>:<em>servname</em>.</p>

<p>Note that <a href="Addrinfo.html#method-c-getaddrinfo"><code>Addrinfo.getaddrinfo</code></a> provides the same functionality in an object oriented style.</p>

<p><em>family</em> should be an address family such as: :INET, :INET6, etc.</p>

<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>

<p><em>protocol</em> should be a protocol defined in the family, and defaults to 0 for the family.</p>

<p><em>flags</em> should be bitwise OR of Socket::AI_* constants.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getaddrinfo</span>(<span class="ruby-string">&quot;www.ruby-lang.org&quot;</span>, <span class="ruby-string">&quot;http&quot;</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">:STREAM</span>)
<span class="ruby-comment">#=&gt; [[&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;, 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP</span>

<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getaddrinfo</span>(<span class="ruby-string">&quot;localhost&quot;</span>, <span class="ruby-keyword">nil</span>)
<span class="ruby-comment">#=&gt; [[&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 1, 6],  # PF_INET/SOCK_STREAM/IPPROTO_TCP</span>
<span class="ruby-comment">#    [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP</span>
<span class="ruby-comment">#    [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 3, 0]]  # PF_INET/SOCK_RAW/IPPROTO_IP</span>
</pre>

<p><em>reverse_lookup</em> directs the form of the third element, and has to be one of below.  If <em>reverse_lookup</em> is omitted, the default value is <code>nil</code>.</p>

<pre>+true+, +:hostname+:  hostname is obtained from numeric address using reverse lookup, which may take a time.
+false+, +:numeric+:  hostname is the same as numeric address.
+nil+:              obey to the current +do_not_reverse_lookup+ flag.</pre>

<p>If <a href="Addrinfo.html"><code>Addrinfo</code></a> object is preferred, use <a href="Addrinfo.html#method-c-getaddrinfo"><code>Addrinfo.getaddrinfo</code></a>.</p>

                              <div class="method-source-code" id="getaddrinfo-source">
            <pre>static VALUE
sock_s_getaddrinfo(int argc, VALUE *argv, VALUE _)
{
    VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
    struct addrinfo hints;
    struct rb_addrinfo *res;
    int norevlookup;

    rb_scan_args(argc, argv, &quot;25&quot;, &amp;host, &amp;port, &amp;family, &amp;socktype, &amp;protocol, &amp;flags, &amp;revlookup);

    MEMZERO(&amp;hints, struct addrinfo, 1);
    hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);

    if (!NIL_P(socktype)) {
        hints.ai_socktype = rsock_socktype_arg(socktype);
    }
    if (!NIL_P(protocol)) {
        hints.ai_protocol = NUM2INT(protocol);
    }
    if (!NIL_P(flags)) {
        hints.ai_flags = NUM2INT(flags);
    }
    if (NIL_P(revlookup) || !rsock_revlookup_flag(revlookup, &amp;norevlookup)) {
        norevlookup = rsock_do_not_reverse_lookup;
    }

    res = rsock_getaddrinfo(host, port, &amp;hints, 0);

    ret = make_addrinfo(res, norevlookup);
    rb_freeaddrinfo(res);
    return ret;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-gethostbyaddr" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          gethostbyaddr(address_string [, address_family]) &rarr; hostent
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Use <a href="Addrinfo.html#method-i-getnameinfo"><code>Addrinfo#getnameinfo</code></a> instead. This method is deprecated for the following reasons:</p>
<ul><li>
<p>Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.</p>
</li><li>
<p>gethostbyaddr() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)</p>
</li><li>
<p>This method uses gethostbyname() function already removed from POSIX.</p>
</li></ul>

<p>This method obtains the host information for <em>address</em>.</p>

<pre>p Socket.gethostbyaddr([221,186,184,68].pack(&quot;CCCC&quot;))
#=&gt; [&quot;carbon.ruby-lang.org&quot;, [], 2, &quot;\xDD\xBA\xB8D&quot;]

p Socket.gethostbyaddr([127,0,0,1].pack(&quot;CCCC&quot;))
[&quot;localhost&quot;, [], 2, &quot;\x7F\x00\x00\x01&quot;]
p Socket.gethostbyaddr(([0]*15+[1]).pack(&quot;C&quot;*16))
#=&gt; [&quot;localhost&quot;, [&quot;ip6-localhost&quot;, &quot;ip6-loopback&quot;], 10,
     &quot;\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01&quot;]</pre>

                              <div class="method-source-code" id="gethostbyaddr-source">
            <pre>static VALUE
sock_s_gethostbyaddr(int argc, VALUE *argv, VALUE _)
{
    VALUE addr, family;
    struct hostent *h;
    char **pch;
    VALUE ary, names;
    int t = AF_INET;

    rb_warn(&quot;Socket.gethostbyaddr is deprecated; use Addrinfo#getnameinfo instead.&quot;);

    rb_scan_args(argc, argv, &quot;11&quot;, &amp;addr, &amp;family);
    StringValue(addr);
    if (!NIL_P(family)) {
        t = rsock_family_arg(family);
    }
#ifdef AF_INET6
    else if (RSTRING_LEN(addr) == 16) {
        t = AF_INET6;
    }
#endif
    h = gethostbyaddr(RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), t);
    if (h == NULL) {
#ifdef HAVE_HSTRERROR
        extern int h_errno;
        rb_raise(rb_eSocket, &quot;%s&quot;, (char*)hstrerror(h_errno));
#else
        rb_raise(rb_eSocket, &quot;host not found&quot;);
#endif
    }
    ary = rb_ary_new();
    rb_ary_push(ary, rb_str_new2(h-&gt;h_name));
    names = rb_ary_new();
    rb_ary_push(ary, names);
    if (h-&gt;h_aliases != NULL) {
        for (pch = h-&gt;h_aliases; *pch; pch++) {
            rb_ary_push(names, rb_str_new2(*pch));
        }
    }
    rb_ary_push(ary, INT2NUM(h-&gt;h_addrtype));
#ifdef h_addr
    for (pch = h-&gt;h_addr_list; *pch; pch++) {
        rb_ary_push(ary, rb_str_new(*pch, h-&gt;h_length));
    }
#else
    rb_ary_push(ary, rb_str_new(h-&gt;h_addr, h-&gt;h_length));
#endif

    return ary;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-gethostbyname" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          gethostbyname(hostname) &rarr; [official_hostname, alias_hostnames, address_family, *address_list]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Use <a href="Addrinfo.html#method-c-getaddrinfo"><code>Addrinfo.getaddrinfo</code></a> instead. This method is deprecated for the following reasons:</p>
<ul><li>
<p>The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.</p>
</li><li>
<p>Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.</p>
</li><li>
<p>gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)</p>
</li><li>
<p>This method uses gethostbyname() function already removed from POSIX.</p>
</li></ul>

<p>This method obtains the host information for <em>hostname</em>.</p>

<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">gethostbyname</span>(<span class="ruby-string">&quot;hal&quot;</span>) <span class="ruby-comment">#=&gt; [&quot;localhost&quot;, [&quot;hal&quot;], 2, &quot;\x7F\x00\x00\x01&quot;]</span>
</pre>

                              <div class="method-source-code" id="gethostbyname-source">
            <pre>static VALUE
sock_s_gethostbyname(VALUE obj, VALUE host)
{
    rb_warn(&quot;Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.&quot;);
    struct rb_addrinfo *res =
        rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
    return rsock_make_hostent(host, res, sock_sockaddr);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-gethostname" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          gethostname &rarr; hostname
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the hostname.</p>

<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">gethostname</span> <span class="ruby-comment">#=&gt; &quot;hal&quot;</span>
</pre>

<p>Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc. If you need local IP address, use <a href="Socket.html#method-c-ip_address_list"><code>Socket.ip_address_list</code></a>.</p>

                              <div class="method-source-code" id="gethostname-source">
            <pre>static VALUE
sock_gethostname(VALUE obj)
{
#if defined(NI_MAXHOST)
#  define RUBY_MAX_HOST_NAME_LEN NI_MAXHOST
#elif defined(HOST_NAME_MAX)
#  define RUBY_MAX_HOST_NAME_LEN HOST_NAME_MAX
#else
#  define RUBY_MAX_HOST_NAME_LEN 1024
#endif

    long len = RUBY_MAX_HOST_NAME_LEN;
    VALUE name;

    name = rb_str_new(0, len);
    while (gethostname(RSTRING_PTR(name), len) &lt; 0) {
        int e = errno;
        switch (e) {
          case ENAMETOOLONG:
#ifdef __linux__
          case EINVAL:
            /* glibc before version 2.1 uses EINVAL instead of ENAMETOOLONG */
#endif
            break;
          default:
            rb_syserr_fail(e, &quot;gethostname(3)&quot;);
        }
        rb_str_modify_expand(name, len);
        len += len;
    }
    rb_str_resize(name, strlen(RSTRING_PTR(name)));
    return name;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-getifaddrs" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          getifaddrs &rarr; [ifaddr1, ...]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of interface addresses. An element of the array is an instance of <a href="Socket/Ifaddr.html"><code>Socket::Ifaddr</code></a>.</p>

<p>This method can be used to find multicast-enabled interfaces:</p>

<pre class="ruby"><span class="ruby-identifier">pp</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getifaddrs</span>.<span class="ruby-identifier">reject</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">ifaddr</span><span class="ruby-operator">|</span>
  <span class="ruby-operator">!</span><span class="ruby-identifier">ifaddr</span>.<span class="ruby-identifier">addr</span>.<span class="ruby-identifier">ip?</span> <span class="ruby-operator">||</span> (<span class="ruby-identifier">ifaddr</span>.<span class="ruby-identifier">flags</span> <span class="ruby-operator">&amp;</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">IFF_MULTICAST</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>)
}.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">ifaddr</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">ifaddr</span>.<span class="ruby-identifier">name</span>, <span class="ruby-identifier">ifaddr</span>.<span class="ruby-identifier">ifindex</span>, <span class="ruby-identifier">ifaddr</span>.<span class="ruby-identifier">addr</span>] }
<span class="ruby-comment">#=&gt; [[&quot;eth0&quot;, 2, #&lt;Addrinfo: 221.186.184.67&gt;],</span>
<span class="ruby-comment">#    [&quot;eth0&quot;, 2, #&lt;Addrinfo: fe80::216:3eff:fe95:88bb%eth0&gt;]]</span>
</pre>

<p>Example result on GNU/Linux:</p>

<pre class="ruby"><span class="ruby-identifier">pp</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getifaddrs</span>
<span class="ruby-comment">#=&gt; [#&lt;Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 PACKET[protocol=0 lo hatype=772 HOST hwaddr=00:00:00:00:00:00]&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=00:16:3e:95:88:bb] broadcast=PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=ff:ff:ff:ff:ff:ff]&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr sit0 NOARP PACKET[protocol=0 sit0 hatype=776 HOST hwaddr=00:00:00:00]&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 127.0.0.1 netmask=255.0.0.0&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 221.186.184.67 netmask=255.255.255.240 broadcast=221.186.184.79&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr lo UP,LOOPBACK,RUNNING,0x10000 ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000 fe80::216:3eff:fe95:88bb%eth0 netmask=ffff:ffff:ffff:ffff::&gt;]</span>
</pre>

<p>Example result on FreeBSD:</p>

<pre class="ruby"><span class="ruby-identifier">pp</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getifaddrs</span>
<span class="ruby-comment">#=&gt; [#&lt;Socket::Ifaddr usbus0 UP,0x10000 LINK[usbus0]&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 LINK[re0 3a:d0:40:9a:fe:e8]&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 10.250.10.18 netmask=255.255.255.? (7 bytes for 16 bytes sockaddr_in) broadcast=10.250.10.255&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 fe80:2::38d0:40ff:fe9a:fee8 netmask=ffff:ffff:ffff:ffff::&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr re0 UP,BROADCAST,RUNNING,MULTICAST,0x800 2001:2e8:408:10::12 netmask=UNSPEC&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr plip0 POINTOPOINT,MULTICAST,0x800 LINK[plip0]&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST LINK[lo0]&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST ::1 netmask=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST fe80:4::1 netmask=ffff:ffff:ffff:ffff::&gt;,</span>
<span class="ruby-comment">#    #&lt;Socket::Ifaddr lo0 UP,LOOPBACK,RUNNING,MULTICAST 127.0.0.1 netmask=255.?.?.? (5 bytes for 16 bytes sockaddr_in)&gt;]</span>
</pre>

                              <div class="method-source-code" id="getifaddrs-source">
            <pre>static VALUE
socket_s_getifaddrs(VALUE self)
{
    return rsock_getifaddrs();
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-getnameinfo" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          getnameinfo(sockaddr [, flags]) &rarr; [hostname, servicename]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Obtains name information for <em>sockaddr</em>.</p>

<p><em>sockaddr</em> should be one of follows.</p>
<ul><li>
<p>packed sockaddr string such as <a href="Socket.html#method-c-sockaddr_in"><code>Socket.sockaddr_in</code></a>(80, “127.0.0.1”)</p>
</li><li>
<p>3-elements array such as [“AF_INET”, 80, “127.0.0.1”]</p>
</li><li>
<p>4-elements array such as [“AF_INET”, 80, ignored, “127.0.0.1”]</p>
</li></ul>

<p><em>flags</em> should be bitwise OR of Socket::NI_* constants.</p>

<p>Note: The last form is compatible with <a href="IPSocket.html#method-i-addr"><code>IPSocket#addr</code></a> and <a href="IPSocket.html#method-i-peeraddr"><code>IPSocket#peeraddr</code></a>.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getnameinfo</span>(<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&quot;127.0.0.1&quot;</span>))       <span class="ruby-comment">#=&gt; [&quot;localhost&quot;, &quot;www&quot;]</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getnameinfo</span>([<span class="ruby-string">&quot;AF_INET&quot;</span>, <span class="ruby-value">80</span>, <span class="ruby-string">&quot;127.0.0.1&quot;</span>])              <span class="ruby-comment">#=&gt; [&quot;localhost&quot;, &quot;www&quot;]</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getnameinfo</span>([<span class="ruby-string">&quot;AF_INET&quot;</span>, <span class="ruby-value">80</span>, <span class="ruby-string">&quot;localhost&quot;</span>, <span class="ruby-string">&quot;127.0.0.1&quot;</span>]) <span class="ruby-comment">#=&gt; [&quot;localhost&quot;, &quot;www&quot;]</span>
</pre>

<p>If <a href="Addrinfo.html"><code>Addrinfo</code></a> object is preferred, use <a href="Addrinfo.html#method-i-getnameinfo"><code>Addrinfo#getnameinfo</code></a>.</p>

                              <div class="method-source-code" id="getnameinfo-source">
            <pre>static VALUE
sock_s_getnameinfo(int argc, VALUE *argv, VALUE _)
{
    VALUE sa, af = Qnil, host = Qnil, port = Qnil, flags, tmp;
    char hbuf[1024], pbuf[1024];
    int fl;
    struct rb_addrinfo *res = NULL;
    struct addrinfo hints, *r;
    int error, saved_errno;
    union_sockaddr ss;
    struct sockaddr *sap;
    socklen_t salen;

    sa = flags = Qnil;
    rb_scan_args(argc, argv, &quot;11&quot;, &amp;sa, &amp;flags);

    fl = 0;
    if (!NIL_P(flags)) {
        fl = NUM2INT(flags);
    }
    tmp = rb_check_sockaddr_string_type(sa);
    if (!NIL_P(tmp)) {
        sa = tmp;
        if (sizeof(ss) &lt; (size_t)RSTRING_LEN(sa)) {
            rb_raise(rb_eTypeError, &quot;sockaddr length too big&quot;);
        }
        memcpy(&amp;ss, RSTRING_PTR(sa), RSTRING_LEN(sa));
        if (!VALIDATE_SOCKLEN(&amp;ss.addr, RSTRING_LEN(sa))) {
            rb_raise(rb_eTypeError, &quot;sockaddr size differs - should not happen&quot;);
        }
        sap = &amp;ss.addr;
        salen = RSTRING_SOCKLEN(sa);
        goto call_nameinfo;
    }
    tmp = rb_check_array_type(sa);
    if (!NIL_P(tmp)) {
        sa = tmp;
        MEMZERO(&amp;hints, struct addrinfo, 1);
        if (RARRAY_LEN(sa) == 3) {
            af = RARRAY_AREF(sa, 0);
            port = RARRAY_AREF(sa, 1);
            host = RARRAY_AREF(sa, 2);
        }
        else if (RARRAY_LEN(sa) &gt;= 4) {
            af = RARRAY_AREF(sa, 0);
            port = RARRAY_AREF(sa, 1);
            host = RARRAY_AREF(sa, 3);
            if (NIL_P(host)) {
                host = RARRAY_AREF(sa, 2);
            }
            else {
                /*
                 * 4th element holds numeric form, don&#39;t resolve.
                 * see rsock_ipaddr().
                 */
#ifdef AI_NUMERICHOST /* AIX 4.3.3 doesn&#39;t have AI_NUMERICHOST. */
                hints.ai_flags |= AI_NUMERICHOST;
#endif
            }
        }
        else {
            rb_raise(rb_eArgError, &quot;array size should be 3 or 4, %ld given&quot;,
                     RARRAY_LEN(sa));
        }
        hints.ai_socktype = (fl &amp; NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
        /* af */
        hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
        res = rsock_getaddrinfo(host, port, &amp;hints, 0);
        sap = res-&gt;ai-&gt;ai_addr;
        salen = res-&gt;ai-&gt;ai_addrlen;
    }
    else {
        rb_raise(rb_eTypeError, &quot;expecting String or Array&quot;);
    }

  call_nameinfo:
    error = rb_getnameinfo(sap, salen, hbuf, sizeof(hbuf),
                           pbuf, sizeof(pbuf), fl);
    if (error) goto error_exit_name;
    if (res) {
        for (r = res-&gt;ai-&gt;ai_next; r; r = r-&gt;ai_next) {
            char hbuf2[1024], pbuf2[1024];

            sap = r-&gt;ai_addr;
            salen = r-&gt;ai_addrlen;
            error = rb_getnameinfo(sap, salen, hbuf2, sizeof(hbuf2),
                                   pbuf2, sizeof(pbuf2), fl);
            if (error) goto error_exit_name;
            if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
                rb_freeaddrinfo(res);
                rb_raise(rb_eSocket, &quot;sockaddr resolved to multiple nodename&quot;);
            }
        }
        rb_freeaddrinfo(res);
    }
    return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));

  error_exit_name:
    saved_errno = errno;
    if (res) rb_freeaddrinfo(res);
    errno = saved_errno;
    rsock_raise_socket_error(&quot;getnameinfo&quot;, error);

    UNREACHABLE_RETURN(Qnil);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-getservbyname" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          getservbyname(service_name)                &rarr; port_number
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          getservbyname(service_name, protocol_name) &rarr; port_number
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Obtains the port number for <em>service_name</em>.</p>

<p>If <em>protocol_name</em> is not given, “tcp” is assumed.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getservbyname</span>(<span class="ruby-string">&quot;smtp&quot;</span>)          <span class="ruby-comment">#=&gt; 25</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getservbyname</span>(<span class="ruby-string">&quot;shell&quot;</span>)         <span class="ruby-comment">#=&gt; 514</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getservbyname</span>(<span class="ruby-string">&quot;syslog&quot;</span>, <span class="ruby-string">&quot;udp&quot;</span>) <span class="ruby-comment">#=&gt; 514</span>
</pre>

                              <div class="method-source-code" id="getservbyname-source">
            <pre>static VALUE
sock_s_getservbyname(int argc, VALUE *argv, VALUE _)
{
    VALUE service, proto;
    struct servent *sp;
    long port;
    const char *servicename, *protoname = &quot;tcp&quot;;

    rb_scan_args(argc, argv, &quot;11&quot;, &amp;service, &amp;proto);
    StringValue(service);
    if (!NIL_P(proto)) StringValue(proto);
    servicename = StringValueCStr(service);
    if (!NIL_P(proto)) protoname = StringValueCStr(proto);
    sp = getservbyname(servicename, protoname);
    if (sp) {
        port = ntohs(sp-&gt;s_port);
    }
    else {
        char *end;

        port = STRTOUL(servicename, &amp;end, 0);
        if (*end != &#39;\0&#39;) {
            rb_raise(rb_eSocket, &quot;no such service %s/%s&quot;, servicename, protoname);
        }
    }
    return INT2FIX(port);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-getservbyport" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          getservbyport(port [, protocol_name]) &rarr; service
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Obtains the port number for <em>port</em>.</p>

<p>If <em>protocol_name</em> is not given, “tcp” is assumed.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getservbyport</span>(<span class="ruby-value">80</span>)         <span class="ruby-comment">#=&gt; &quot;www&quot;</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getservbyport</span>(<span class="ruby-value">514</span>, <span class="ruby-string">&quot;tcp&quot;</span>) <span class="ruby-comment">#=&gt; &quot;shell&quot;</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">getservbyport</span>(<span class="ruby-value">514</span>, <span class="ruby-string">&quot;udp&quot;</span>) <span class="ruby-comment">#=&gt; &quot;syslog&quot;</span>
</pre>

                              <div class="method-source-code" id="getservbyport-source">
            <pre>static VALUE
sock_s_getservbyport(int argc, VALUE *argv, VALUE _)
{
    VALUE port, proto;
    struct servent *sp;
    long portnum;
    const char *protoname = &quot;tcp&quot;;

    rb_scan_args(argc, argv, &quot;11&quot;, &amp;port, &amp;proto);
    portnum = NUM2LONG(port);
    if (portnum != (uint16_t)portnum) {
        const char *s = portnum &gt; 0 ? &quot;big&quot; : &quot;small&quot;;
        rb_raise(rb_eRangeError, &quot;integer %ld too %s to convert into `int16_t&#39;&quot;, portnum, s);
    }
    if (!NIL_P(proto)) protoname = StringValueCStr(proto);

    sp = getservbyport((int)htons((uint16_t)portnum), protoname);
    if (!sp) {
        rb_raise(rb_eSocket, &quot;no such service for port %d/%s&quot;, (int)portnum, protoname);
    }
    return rb_str_new2(sp-&gt;s_name);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-ip_address_list" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          ip_address_list &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns local IP addresses as an array.</p>

<p>The array contains <a href="Addrinfo.html"><code>Addrinfo</code></a> objects.</p>

<pre>pp Socket.ip_address_list
#=&gt; [#&lt;Addrinfo: 127.0.0.1&gt;,
     #&lt;Addrinfo: 192.168.0.128&gt;,
     #&lt;Addrinfo: ::1&gt;,
     ...]</pre>

                              <div class="method-source-code" id="ip_address_list-source">
            <pre>static VALUE
socket_s_ip_address_list(VALUE self)
{
#if defined(HAVE_GETIFADDRS)
    struct ifaddrs *ifp = NULL;
    struct ifaddrs *p;
    int ret;
    VALUE list;

    ret = getifaddrs(&amp;ifp);
    if (ret == -1) {
        rb_sys_fail(&quot;getifaddrs&quot;);
    }

    list = rb_ary_new();
    for (p = ifp; p; p = p-&gt;ifa_next) {
        if (p-&gt;ifa_addr != NULL &amp;&amp; IS_IP_FAMILY(p-&gt;ifa_addr-&gt;sa_family)) {
            struct sockaddr *addr = p-&gt;ifa_addr;
#if defined(AF_INET6) &amp;&amp; defined(__sun)
            /*
             * OpenIndiana SunOS 5.11 getifaddrs() returns IPv6 link local
             * address with sin6_scope_id == 0.
             * So fill it from the interface name (ifa_name).
             */
            struct sockaddr_in6 addr6;
            if (addr-&gt;sa_family == AF_INET6) {
                socklen_t len = (socklen_t)sizeof(struct sockaddr_in6);
                memcpy(&amp;addr6, addr, len);
                addr = (struct sockaddr *)&amp;addr6;
                if (IN6_IS_ADDR_LINKLOCAL(&amp;addr6.sin6_addr) &amp;&amp;
                    addr6.sin6_scope_id == 0) {
                    unsigned int ifindex = if_nametoindex(p-&gt;ifa_name);
                    if (ifindex != 0) {
                        addr6.sin6_scope_id = ifindex;
                    }
                }
            }
#endif
            rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
        }
    }

    freeifaddrs(ifp);

    return list;
#elif defined(SIOCGLIFCONF) &amp;&amp; defined(SIOCGLIFNUM)
    /* Solaris if_tcp(7P) */
    int fd = -1;
    int ret;
    struct lifnum ln;
    struct lifconf lc;
    const char *reason = NULL;
    int save_errno;
    int i;
    VALUE list = Qnil;

    lc.lifc_buf = NULL;

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd == -1)
        rb_sys_fail(&quot;socket(2)&quot;);

    memset(&amp;ln, 0, sizeof(ln));
    ln.lifn_family = AF_UNSPEC;

    ret = ioctl(fd, SIOCGLIFNUM, &amp;ln);
    if (ret == -1) {
        reason = &quot;SIOCGLIFNUM&quot;;
        goto finish;
    }

    memset(&amp;lc, 0, sizeof(lc));
    lc.lifc_family = AF_UNSPEC;
    lc.lifc_flags = 0;
    lc.lifc_len = sizeof(struct lifreq) * ln.lifn_count;
    lc.lifc_req = xmalloc(lc.lifc_len);

    ret = ioctl(fd, SIOCGLIFCONF, &amp;lc);
    if (ret == -1) {
        reason = &quot;SIOCGLIFCONF&quot;;
        goto finish;
    }

    list = rb_ary_new();
    for (i = 0; i &lt; ln.lifn_count; i++) {
        struct lifreq *req = &amp;lc.lifc_req[i];
        if (IS_IP_FAMILY(req-&gt;lifr_addr.ss_family)) {
            if (req-&gt;lifr_addr.ss_family == AF_INET6 &amp;&amp;
                IN6_IS_ADDR_LINKLOCAL(&amp;((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_addr) &amp;&amp;
                ((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_scope_id == 0) {
                struct lifreq req2;
                memcpy(req2.lifr_name, req-&gt;lifr_name, LIFNAMSIZ);
                ret = ioctl(fd, SIOCGLIFINDEX, &amp;req2);
                if (ret == -1) {
                    reason = &quot;SIOCGLIFINDEX&quot;;
                    goto finish;
                }
                ((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_scope_id = req2.lifr_index;
            }
            rb_ary_push(list, sockaddr_obj((struct sockaddr *)&amp;req-&gt;lifr_addr, req-&gt;lifr_addrlen));
        }
    }

  finish:
    save_errno = errno;
    xfree(lc.lifc_req);
    if (fd != -1)
        close(fd);
    errno = save_errno;

    if (reason)
        rb_syserr_fail(save_errno, reason);
    return list;

#elif defined(SIOCGIFCONF)
    int fd = -1;
    int ret;
#define EXTRA_SPACE ((int)(sizeof(struct ifconf) + sizeof(union_sockaddr)))
    char initbuf[4096+EXTRA_SPACE];
    char *buf = initbuf;
    int bufsize;
    struct ifconf conf;
    struct ifreq *req;
    VALUE list = Qnil;
    const char *reason = NULL;
    int save_errno;

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd == -1)
        rb_sys_fail(&quot;socket(2)&quot;);

    bufsize = sizeof(initbuf);
    buf = initbuf;

  retry:
    conf.ifc_len = bufsize;
    conf.ifc_req = (struct ifreq *)buf;

    /* fprintf(stderr, &quot;bufsize: %d\n&quot;, bufsize); */

    ret = ioctl(fd, SIOCGIFCONF, &amp;conf);
    if (ret == -1) {
        reason = &quot;SIOCGIFCONF&quot;;
        goto finish;
    }

    /* fprintf(stderr, &quot;conf.ifc_len: %d\n&quot;, conf.ifc_len); */

    if (bufsize - EXTRA_SPACE &lt; conf.ifc_len) {
        if (bufsize &lt; conf.ifc_len) {
            /* NetBSD returns required size for all interfaces. */
            bufsize = conf.ifc_len + EXTRA_SPACE;
        }
        else {
            bufsize = bufsize &lt;&lt; 1;
        }
        if (buf == initbuf)
            buf = NULL;
        buf = xrealloc(buf, bufsize);
        goto retry;
    }

    close(fd);
    fd = -1;

    list = rb_ary_new();
    req = conf.ifc_req;
    while ((char*)req &lt; (char*)conf.ifc_req + conf.ifc_len) {
        struct sockaddr *addr = &amp;req-&gt;ifr_addr;
        if (IS_IP_FAMILY(addr-&gt;sa_family)) {
            rb_ary_push(list, sockaddr_obj(addr, sockaddr_len(addr)));
        }
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
# ifndef _SIZEOF_ADDR_IFREQ
#  define _SIZEOF_ADDR_IFREQ(r) \
          (sizeof(struct ifreq) + \
           (sizeof(struct sockaddr) &lt; (r).ifr_addr.sa_len ? \
            (r).ifr_addr.sa_len - sizeof(struct sockaddr) : \
            0))
# endif
        req = (struct ifreq *)((char*)req + _SIZEOF_ADDR_IFREQ(*req));
#else
        req = (struct ifreq *)((char*)req + sizeof(struct ifreq));
#endif
    }

  finish:

    save_errno = errno;
    if (buf != initbuf)
        xfree(buf);
    if (fd != -1)
        close(fd);
    errno = save_errno;

    if (reason)
        rb_syserr_fail(save_errno, reason);
    return list;

#undef EXTRA_SPACE
#elif defined(_WIN32)
    typedef struct ip_adapter_unicast_address_st {
        unsigned LONG_LONG dummy0;
        struct ip_adapter_unicast_address_st *Next;
        struct {
            struct sockaddr *lpSockaddr;
            int iSockaddrLength;
        } Address;
        int dummy1;
        int dummy2;
        int dummy3;
        long dummy4;
        long dummy5;
        long dummy6;
    } ip_adapter_unicast_address_t;
    typedef struct ip_adapter_anycast_address_st {
        unsigned LONG_LONG dummy0;
        struct ip_adapter_anycast_address_st *Next;
        struct {
            struct sockaddr *lpSockaddr;
            int iSockaddrLength;
        } Address;
    } ip_adapter_anycast_address_t;
    typedef struct ip_adapter_addresses_st {
        unsigned LONG_LONG dummy0;
        struct ip_adapter_addresses_st *Next;
        void *dummy1;
        ip_adapter_unicast_address_t *FirstUnicastAddress;
        ip_adapter_anycast_address_t *FirstAnycastAddress;
        void *dummy2;
        void *dummy3;
        void *dummy4;
        void *dummy5;
        void *dummy6;
        BYTE dummy7[8];
        DWORD dummy8;
        DWORD dummy9;
        DWORD dummy10;
        DWORD IfType;
        int OperStatus;
        DWORD dummy12;
        DWORD dummy13[16];
        void *dummy14;
    } ip_adapter_addresses_t;
    typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG, ULONG, PVOID, ip_adapter_addresses_t *, PULONG);
    HMODULE h;
    GetAdaptersAddresses_t pGetAdaptersAddresses;
    ULONG len;
    DWORD ret;
    ip_adapter_addresses_t *adapters;
    VALUE list;

    h = LoadLibrary(&quot;iphlpapi.dll&quot;);
    if (!h)
        rb_notimplement();
    pGetAdaptersAddresses = (GetAdaptersAddresses_t)GetProcAddress(h, &quot;GetAdaptersAddresses&quot;);
    if (!pGetAdaptersAddresses) {
        FreeLibrary(h);
        rb_notimplement();
    }

    ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &amp;len);
    if (ret != ERROR_SUCCESS &amp;&amp; ret != ERROR_BUFFER_OVERFLOW) {
        errno = rb_w32_map_errno(ret);
        FreeLibrary(h);
        rb_sys_fail(&quot;GetAdaptersAddresses&quot;);
    }
    adapters = (ip_adapter_addresses_t *)ALLOCA_N(BYTE, len);
    ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, adapters, &amp;len);
    if (ret != ERROR_SUCCESS) {
        errno = rb_w32_map_errno(ret);
        FreeLibrary(h);
        rb_sys_fail(&quot;GetAdaptersAddresses&quot;);
    }

    list = rb_ary_new();
    for (; adapters; adapters = adapters-&gt;Next) {
        ip_adapter_unicast_address_t *uni;
        ip_adapter_anycast_address_t *any;
        if (adapters-&gt;OperStatus != 1)  /* 1 means IfOperStatusUp */
            continue;
        for (uni = adapters-&gt;FirstUnicastAddress; uni; uni = uni-&gt;Next) {
#ifndef INET6
            if (uni-&gt;Address.lpSockaddr-&gt;sa_family == AF_INET)
#else
            if (IS_IP_FAMILY(uni-&gt;Address.lpSockaddr-&gt;sa_family))
#endif
                rb_ary_push(list, sockaddr_obj(uni-&gt;Address.lpSockaddr, uni-&gt;Address.iSockaddrLength));
        }
        for (any = adapters-&gt;FirstAnycastAddress; any; any = any-&gt;Next) {
#ifndef INET6
            if (any-&gt;Address.lpSockaddr-&gt;sa_family == AF_INET)
#else
            if (IS_IP_FAMILY(any-&gt;Address.lpSockaddr-&gt;sa_family))
#endif
                rb_ary_push(list, sockaddr_obj(any-&gt;Address.lpSockaddr, any-&gt;Address.iSockaddrLength));
        }
    }

    FreeLibrary(h);
    return list;
#endif
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-new" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(domain, socktype [, protocol]) &rarr; socket
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates a new socket object.</p>

<p><em>domain</em> should be a communications domain such as: :INET, :INET6, :UNIX, etc.</p>

<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>

<p><em>protocol</em> is optional and should be a protocol defined in the domain. If protocol is not given, 0 is used internally.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:INET</span>, <span class="ruby-value">:STREAM</span>) <span class="ruby-comment"># TCP socket</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:INET</span>, <span class="ruby-value">:DGRAM</span>)  <span class="ruby-comment"># UDP socket</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:STREAM</span>) <span class="ruby-comment"># UNIX stream socket</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:DGRAM</span>)  <span class="ruby-comment"># UNIX datagram socket</span>
</pre>

                              <div class="method-source-code" id="new-source">
            <pre>static VALUE
sock_initialize(int argc, VALUE *argv, VALUE sock)
{
    VALUE domain, type, protocol;
    int fd;
    int d, t;

    rb_scan_args(argc, argv, &quot;21&quot;, &amp;domain, &amp;type, &amp;protocol);
    if (NIL_P(protocol))
        protocol = INT2FIX(0);

    setup_domain_and_type(domain, &amp;d, type, &amp;t);
    fd = rsock_socket(d, t, NUM2INT(protocol));
    if (fd &lt; 0) rb_sys_fail(&quot;socket(2)&quot;);

    return rsock_init_sock(sock, fd);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pack_sockaddr_in" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sockaddr_in(port, host)      &rarr; sockaddr
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          pack_sockaddr_in(port, host) &rarr; sockaddr
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr string.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&quot;127.0.0.1&quot;</span>)
<span class="ruby-comment">#=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;</span>

<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&quot;::1&quot;</span>)
<span class="ruby-comment">#=&gt; &quot;\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00&quot;</span>
</pre>

                              <div class="method-source-code" id="pack_sockaddr_in-source">
            <pre>static VALUE
sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
{
    struct rb_addrinfo *res = rsock_addrinfo(host, port, AF_UNSPEC, 0, 0);
    VALUE addr = rb_str_new((char*)res-&gt;ai-&gt;ai_addr, res-&gt;ai-&gt;ai_addrlen);

    rb_freeaddrinfo(res);

    return addr;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pack_sockaddr_un" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sockaddr_un(path)      &rarr; sockaddr
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          pack_sockaddr_un(path) &rarr; sockaddr
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Packs <em>path</em> as an <a href="Socket.html#AF_UNIX"><code>AF_UNIX</code></a> sockaddr string.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_un</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>) <span class="ruby-comment">#=&gt; &quot;\x01\x00/tmp/sock\x00\x00...&quot;</span>
</pre>

                              <div class="method-source-code" id="pack_sockaddr_un-source">
            <pre>static VALUE
sock_s_pack_sockaddr_un(VALUE self, VALUE path)
{
    struct sockaddr_un sockaddr;
    VALUE addr;

    StringValue(path);
    INIT_SOCKADDR_UN(&amp;sockaddr, sizeof(struct sockaddr_un));
    if (sizeof(sockaddr.sun_path) &lt; (size_t)RSTRING_LEN(path)) {
        rb_raise(rb_eArgError, &quot;too long unix socket path (%&quot;PRIuSIZE&quot; bytes given but %&quot;PRIuSIZE&quot; bytes max)&quot;,
            (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path));
    }
    memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
    addr = rb_str_new((char*)&amp;sockaddr, rsock_unix_sockaddr_len(path));

    return addr;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-pair" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          pair(domain, type, protocol)       &rarr; [socket1, socket2]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          socketpair(domain, type, protocol) &rarr; [socket1, socket2]
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Creates a pair of sockets connected each other.</p>

<p><em>domain</em> should be a communications domain such as: :INET, :INET6, :UNIX, etc.</p>

<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>

<p><em>protocol</em> should be a protocol defined in the domain, defaults to 0 for the domain.</p>

<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pair</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;a&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;b&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">close</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;ab&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;&quot;</span>

<span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pair</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;a&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;b&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;a&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;b&quot;</span>
</pre>

                              <div class="method-source-code" id="pair-source">
            <pre>VALUE
rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
{
    VALUE domain, type, protocol;
    int d, t, p, sp[2];
    int ret;
    VALUE s1, s2, r;

    rb_scan_args(argc, argv, &quot;21&quot;, &amp;domain, &amp;type, &amp;protocol);
    if (NIL_P(protocol))
        protocol = INT2FIX(0);

    setup_domain_and_type(domain, &amp;d, type, &amp;t);
    p = NUM2INT(protocol);
    ret = rsock_socketpair(d, t, p, sp);
    if (ret &lt; 0) {
        rb_sys_fail(&quot;socketpair(2)&quot;);
    }

    s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
    s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
    r = rb_assoc_new(s1, s2);
    if (rb_block_given_p()) {
        return rb_ensure(pair_yield, r, io_close, s1);
    }
    return r;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-sockaddr_in" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sockaddr_in(port, host)      &rarr; sockaddr
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          pack_sockaddr_in(port, host) &rarr; sockaddr
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr string.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&quot;127.0.0.1&quot;</span>)
<span class="ruby-comment">#=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;</span>

<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&quot;::1&quot;</span>)
<span class="ruby-comment">#=&gt; &quot;\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00&quot;</span>
</pre>

                              <div class="method-source-code" id="sockaddr_in-source">
            <pre>static VALUE
sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
{
    struct rb_addrinfo *res = rsock_addrinfo(host, port, AF_UNSPEC, 0, 0);
    VALUE addr = rb_str_new((char*)res-&gt;ai-&gt;ai_addr, res-&gt;ai-&gt;ai_addrlen);

    rb_freeaddrinfo(res);

    return addr;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-sockaddr_un" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sockaddr_un(path)      &rarr; sockaddr
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          pack_sockaddr_un(path) &rarr; sockaddr
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Packs <em>path</em> as an <a href="Socket.html#AF_UNIX"><code>AF_UNIX</code></a> sockaddr string.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_un</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>) <span class="ruby-comment">#=&gt; &quot;\x01\x00/tmp/sock\x00\x00...&quot;</span>
</pre>

                              <div class="method-source-code" id="sockaddr_un-source">
            <pre>static VALUE
sock_s_pack_sockaddr_un(VALUE self, VALUE path)
{
    struct sockaddr_un sockaddr;
    VALUE addr;

    StringValue(path);
    INIT_SOCKADDR_UN(&amp;sockaddr, sizeof(struct sockaddr_un));
    if (sizeof(sockaddr.sun_path) &lt; (size_t)RSTRING_LEN(path)) {
        rb_raise(rb_eArgError, &quot;too long unix socket path (%&quot;PRIuSIZE&quot; bytes given but %&quot;PRIuSIZE&quot; bytes max)&quot;,
            (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path));
    }
    memcpy(sockaddr.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
    addr = rb_str_new((char*)&amp;sockaddr, rsock_unix_sockaddr_len(path));

    return addr;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-socketpair" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          pair(domain, type, protocol)       &rarr; [socket1, socket2]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          socketpair(domain, type, protocol) &rarr; [socket1, socket2]
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Creates a pair of sockets connected each other.</p>

<p><em>domain</em> should be a communications domain such as: :INET, :INET6, :UNIX, etc.</p>

<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>

<p><em>protocol</em> should be a protocol defined in the domain, defaults to 0 for the domain.</p>

<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pair</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;a&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;b&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">close</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;ab&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;&quot;</span>

<span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pair</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;a&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;b&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;a&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;b&quot;</span>
</pre>

                              <div class="method-source-code" id="socketpair-source">
            <pre>VALUE
rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
{
    VALUE domain, type, protocol;
    int d, t, p, sp[2];
    int ret;
    VALUE s1, s2, r;

    rb_scan_args(argc, argv, &quot;21&quot;, &amp;domain, &amp;type, &amp;protocol);
    if (NIL_P(protocol))
        protocol = INT2FIX(0);

    setup_domain_and_type(domain, &amp;d, type, &amp;t);
    p = NUM2INT(protocol);
    ret = rsock_socketpair(d, t, p, sp);
    if (ret &lt; 0) {
        rb_sys_fail(&quot;socketpair(2)&quot;);
    }

    s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
    s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
    r = rb_assoc_new(s1, s2);
    if (rb_block_given_p()) {
        return rb_ensure(pair_yield, r, io_close, s1);
    }
    return r;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-tcp" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          tcp(host, port, local_host=nil, local_port=nil, [opts]) {|socket| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          tcp(host, port, local_host=nil, local_port=nil, [opts])
                              </span>
                            </div>

                            <div class="method-description">
                              <p>creates a new socket object connected to host:port using TCP/IP.</p>

<p>If local_host:local_port is given, the socket is bound to it.</p>

<p>The optional last argument <em>opts</em> is options represented by a hash. <em>opts</em> may have following options:</p>
<dl class="rdoc-list label-list"><dt>:connect_timeout
<dd>
<p>specify the timeout in seconds.</p>
</dd></dl>

<p>If a block is given, the block is called with the socket. The value of the block is returned. The socket is closed when this method returns.</p>

<p>If no block is given, the socket is returned.</p>

<pre class="ruby"><span class="ruby-constant">Socket</span>.<span class="ruby-identifier">tcp</span>(<span class="ruby-string">&quot;www.ruby-lang.org&quot;</span>, <span class="ruby-value">80</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sock</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">print</span> <span class="ruby-string">&quot;GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n&quot;</span>
  <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">close_write</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">read</span>
}
</pre>

                              <div class="method-source-code" id="tcp-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 626</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">tcp</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>, <span class="ruby-identifier">local_host</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">local_port</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-value">connect_timeout:</span> <span class="ruby-keyword">nil</span>, <span class="ruby-value">resolv_timeout:</span> <span class="ruby-keyword">nil</span>) <span class="ruby-comment"># :yield: socket</span>
  <span class="ruby-identifier">last_error</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-identifier">ret</span> = <span class="ruby-keyword">nil</span>

  <span class="ruby-identifier">local_addr_list</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">local_host</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword">nil</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">local_port</span> <span class="ruby-operator">!=</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">local_addr_list</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">getaddrinfo</span>(<span class="ruby-identifier">local_host</span>, <span class="ruby-identifier">local_port</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-value">timeout:</span> <span class="ruby-identifier">resolv_timeout</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">ai</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">local_addr_list</span>
      <span class="ruby-identifier">local_addr</span> = <span class="ruby-identifier">local_addr_list</span>.<span class="ruby-identifier">find</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">local_ai</span><span class="ruby-operator">|</span> <span class="ruby-identifier">local_ai</span>.<span class="ruby-identifier">afamily</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">afamily</span> }
      <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">local_addr</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">local_addr</span> = <span class="ruby-keyword">nil</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-identifier">sock</span> = <span class="ruby-identifier">local_addr</span> <span class="ruby-operator">?</span>
        <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">connect_from</span>(<span class="ruby-identifier">local_addr</span>, <span class="ruby-value">timeout:</span> <span class="ruby-identifier">connect_timeout</span>) <span class="ruby-operator">:</span>
        <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">connect</span>(<span class="ruby-value">timeout:</span> <span class="ruby-identifier">connect_timeout</span>)
    <span class="ruby-keyword">rescue</span> <span class="ruby-constant">SystemCallError</span>
      <span class="ruby-identifier">last_error</span> = <span class="ruby-identifier">$!</span>
      <span class="ruby-keyword">next</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">ret</span> = <span class="ruby-identifier">sock</span>
    <span class="ruby-keyword">break</span>
  }
  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">ret</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">last_error</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-identifier">last_error</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">SocketError</span>, <span class="ruby-string">&quot;no appropriate local address&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">ret</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">ret</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">ret</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-tcp_server_loop" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">tcp_server_loop</span><span
                                class="method-args">(host=nil, port) { |socket, client_addrinfo| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>creates a TCP/IP server on <em>port</em> and calls the block for each connection accepted. The block is called with a socket and a client_address as an <a href="Addrinfo.html"><code>Addrinfo</code></a> object.</p>

<p>If <em>host</em> is specified, it is used with <em>port</em> to determine the server addresses.</p>

<p>The socket is <strong>not</strong> closed when the block returns. So application should close it explicitly.</p>

<p>This method calls the block sequentially. It means that the next connection is not accepted until the block returns. So concurrent mechanism, thread for example, should be used to service multiple clients at a time.</p>

<p>Note that <a href="Addrinfo.html#method-c-getaddrinfo"><code>Addrinfo.getaddrinfo</code></a> is used to determine the server socket addresses. When <a href="Addrinfo.html#method-c-getaddrinfo"><code>Addrinfo.getaddrinfo</code></a> returns two or more addresses, IPv4 and IPv6 address for example, all of them are used. <a href="Socket.html#method-c-tcp_server_loop"><code>Socket.tcp_server_loop</code></a> succeeds if one socket can be used at least.</p>

<pre class="ruby"><span class="ruby-comment"># Sequential echo server.</span>
<span class="ruby-comment"># It services only one client at a time.</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">tcp_server_loop</span>(<span class="ruby-value">16807</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sock</span>, <span class="ruby-identifier">client_addrinfo</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">sock</span>, <span class="ruby-identifier">sock</span>)
  <span class="ruby-keyword">ensure</span>
    <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-keyword">end</span>
}

<span class="ruby-comment"># Threaded echo server</span>
<span class="ruby-comment"># It services multiple clients at a time.</span>
<span class="ruby-comment"># Note that it may accept connections too much.</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">tcp_server_loop</span>(<span class="ruby-value">16807</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sock</span>, <span class="ruby-identifier">client_addrinfo</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> {
    <span class="ruby-keyword">begin</span>
      <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">sock</span>, <span class="ruby-identifier">sock</span>)
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-keyword">end</span>
  }
}
</pre>

                              <div class="method-source-code" id="tcp_server_loop-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 859</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">tcp_server_loop</span>(<span class="ruby-identifier">host</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">port</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>) <span class="ruby-comment"># :yield: socket, client_addrinfo</span>
  <span class="ruby-identifier">tcp_server_sockets</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sockets</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">accept_loop</span>(<span class="ruby-identifier">sockets</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-tcp_server_sockets" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">tcp_server_sockets</span><span
                                class="method-args">(host=nil, port) { |sockets| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>creates TCP/IP server sockets for <em>host</em> and <em>port</em>. <em>host</em> is optional.</p>

<p>If no block given, it returns an array of listening sockets.</p>

<p>If a block is given, the block is called with the sockets. The value of the block is returned. The socket is closed when this method returns.</p>

<p>If <em>port</em> is 0, actual port number is chosen dynamically. However all sockets in the result has same port number.</p>

<pre class="ruby"><span class="ruby-comment"># tcp_server_sockets returns two sockets.</span>
<span class="ruby-identifier">sockets</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">tcp_server_sockets</span>(<span class="ruby-value">1296</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">sockets</span> <span class="ruby-comment">#=&gt; [#&lt;Socket:fd 3&gt;, #&lt;Socket:fd 4&gt;]</span>

<span class="ruby-comment"># The sockets contains IPv6 and IPv4 sockets.</span>
<span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">local_address</span> }
<span class="ruby-comment">#=&gt; #&lt;Addrinfo: [::]:1296 TCP&gt;</span>
<span class="ruby-comment">#   #&lt;Addrinfo: 0.0.0.0:1296 TCP&gt;</span>

<span class="ruby-comment"># IPv6 and IPv4 socket has same port number, 53114, even if it is chosen dynamically.</span>
<span class="ruby-identifier">sockets</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">tcp_server_sockets</span>(<span class="ruby-value">0</span>)
<span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">local_address</span> }
<span class="ruby-comment">#=&gt; #&lt;Addrinfo: [::]:53114 TCP&gt;</span>
<span class="ruby-comment">#   #&lt;Addrinfo: 0.0.0.0:53114 TCP&gt;</span>

<span class="ruby-comment"># The block is called with the sockets.</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">tcp_server_sockets</span>(<span class="ruby-value">0</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sockets</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sockets</span> <span class="ruby-comment">#=&gt; [#&lt;Socket:fd 3&gt;, #&lt;Socket:fd 4&gt;]</span>
}
</pre>

                              <div class="method-source-code" id="tcp_server_sockets-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 759</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">tcp_server_sockets</span>(<span class="ruby-identifier">host</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">port</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">port</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
    <span class="ruby-identifier">sockets</span> = <span class="ruby-identifier">tcp_server_sockets_port0</span>(<span class="ruby-identifier">host</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">last_error</span> = <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">sockets</span> = []
    <span class="ruby-keyword">begin</span>
      <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">AI_PASSIVE</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">ai</span><span class="ruby-operator">|</span>
        <span class="ruby-keyword">begin</span>
          <span class="ruby-identifier">s</span> = <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">listen</span>
        <span class="ruby-keyword">rescue</span> <span class="ruby-constant">SystemCallError</span>
          <span class="ruby-identifier">last_error</span> = <span class="ruby-identifier">$!</span>
          <span class="ruby-keyword">next</span>
        <span class="ruby-keyword">end</span>
        <span class="ruby-identifier">sockets</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">s</span>
      }
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">empty?</span>
        <span class="ruby-identifier">raise</span> <span class="ruby-identifier">last_error</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Exception</span>
      <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:close</span>)
      <span class="ruby-identifier">raise</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">sockets</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:close</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">sockets</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-udp_server_loop" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          udp_server_loop(port) {|msg, msg_src| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          udp_server_loop(host, port) {|msg, msg_src| ... }
                              </span>
                            </div>

                            <div class="method-description">
                              <p>creates a UDP/IP server on <em>port</em> and calls the block for each message arrived. The block is called with the message and its source information.</p>

<p>This method allocates sockets internally using <em>port</em>. If <em>host</em> is specified, it is used conjunction with <em>port</em> to determine the server addresses.</p>

<p>The <em>msg</em> is a string.</p>

<p>The <em>msg_src</em> is a <a href="Socket/UDPSource.html"><code>Socket::UDPSource</code></a> object. It is used for reply.</p>

<pre class="ruby"><span class="ruby-comment"># UDP/IP echo server.</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">udp_server_loop</span>(<span class="ruby-value">9261</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">msg</span>, <span class="ruby-identifier">msg_src</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">msg_src</span>.<span class="ruby-identifier">reply</span> <span class="ruby-identifier">msg</span>
}
</pre>

                              <div class="method-source-code" id="udp_server_loop-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 1029</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">udp_server_loop</span>(<span class="ruby-identifier">host</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">port</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>) <span class="ruby-comment"># :yield: message, message_source</span>
  <span class="ruby-identifier">udp_server_sockets</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sockets</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">udp_server_loop_on</span>(<span class="ruby-identifier">sockets</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-udp_server_loop_on" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          udp_server_loop_on(sockets) {|msg, msg_src| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Run UDP/IP server loop on the given sockets.</p>

<p>The return value of <a href="Socket.html#method-c-udp_server_sockets"><code>Socket.udp_server_sockets</code></a> is appropriate for the argument.</p>

<p>It calls the block for each message received.</p>

                              <div class="method-source-code" id="udp_server_loop_on-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 1002</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">udp_server_loop_on</span>(<span class="ruby-identifier">sockets</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>) <span class="ruby-comment"># :yield: msg, msg_src</span>
  <span class="ruby-identifier">loop</span> {
    <span class="ruby-identifier">readable</span>, <span class="ruby-identifier">_</span>, <span class="ruby-identifier">_</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>(<span class="ruby-identifier">sockets</span>)
    <span class="ruby-identifier">udp_server_recv</span>(<span class="ruby-identifier">readable</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-udp_server_recv" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          udp_server_recv(sockets) {|msg, msg_src| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Receive UDP/IP packets from the given <em>sockets</em>. For each packet received, the block is called.</p>

<p>The block receives <em>msg</em> and <em>msg_src</em>. <em>msg</em> is a string which is the payload of the received packet. <em>msg_src</em> is a <a href="Socket/UDPSource.html"><code>Socket::UDPSource</code></a> object which is used for reply.</p>

<p><a href="Socket.html#method-c-udp_server_loop"><code>Socket.udp_server_loop</code></a> can be implemented using this method as follows.</p>

<pre>udp_server_sockets(host, port) {|sockets|
  loop {
    readable, _, _ = IO.select(sockets)
    udp_server_recv(readable) {|msg, msg_src| ... }
  }
}</pre>

                              <div class="method-source-code" id="udp_server_recv-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 975</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">udp_server_recv</span>(<span class="ruby-identifier">sockets</span>)
  <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">msg</span>, <span class="ruby-identifier">sender_addrinfo</span>, <span class="ruby-identifier">_</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">controls</span> = <span class="ruby-identifier">r</span>.<span class="ruby-identifier">recvmsg_nonblock</span>(<span class="ruby-value">exception:</span> <span class="ruby-keyword">false</span>)
    <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">msg</span> <span class="ruby-operator">==</span> <span class="ruby-value">:wait_readable</span>
    <span class="ruby-identifier">ai</span> = <span class="ruby-identifier">r</span>.<span class="ruby-identifier">local_address</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ipv6?</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">pktinfo</span> = <span class="ruby-identifier">controls</span>.<span class="ruby-identifier">find</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">cmsg_is?</span>(<span class="ruby-value">:IPV6</span>, <span class="ruby-value">:PKTINFO</span>) }
      <span class="ruby-identifier">ai</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">udp</span>(<span class="ruby-identifier">pktinfo</span>.<span class="ruby-identifier">ipv6_pktinfo_addr</span>.<span class="ruby-identifier">ip_address</span>, <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ip_port</span>)
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">msg</span>, <span class="ruby-constant">UDPSource</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">sender_addrinfo</span>, <span class="ruby-identifier">ai</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">reply_msg</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">r</span>.<span class="ruby-identifier">sendmsg</span> <span class="ruby-identifier">reply_msg</span>, <span class="ruby-value">0</span>, <span class="ruby-identifier">sender_addrinfo</span>, <span class="ruby-identifier">pktinfo</span>
      }
    <span class="ruby-keyword">else</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">msg</span>, <span class="ruby-constant">UDPSource</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">sender_addrinfo</span>, <span class="ruby-identifier">ai</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">reply_msg</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">r</span>.<span class="ruby-identifier">send</span> <span class="ruby-identifier">reply_msg</span>, <span class="ruby-value">0</span>, <span class="ruby-identifier">sender_addrinfo</span>
      }
    <span class="ruby-keyword">end</span>
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-udp_server_sockets" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          udp_server_sockets([host, ] port)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates UDP/IP sockets for a UDP server.</p>

<p>If no block given, it returns an array of sockets.</p>

<p>If a block is given, the block is called with the sockets. The value of the block is returned. The sockets are closed when this method returns.</p>

<p>If <em>port</em> is zero, some port is chosen. But the chosen port is used for the all sockets.</p>

<pre class="ruby"><span class="ruby-comment"># UDP/IP echo server</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">udp_server_sockets</span>(<span class="ruby-value">0</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sockets</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">local_address</span>.<span class="ruby-identifier">ip_port</span>     <span class="ruby-comment">#=&gt; 32963</span>
  <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">udp_server_loop_on</span>(<span class="ruby-identifier">sockets</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">msg</span>, <span class="ruby-identifier">msg_src</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">msg_src</span>.<span class="ruby-identifier">reply</span> <span class="ruby-identifier">msg</span>
  }
}
</pre>

                              <div class="method-source-code" id="udp_server_sockets-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 887</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">udp_server_sockets</span>(<span class="ruby-identifier">host</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">port</span>)
  <span class="ruby-identifier">last_error</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-identifier">sockets</span> = []

  <span class="ruby-identifier">ipv6_recvpktinfo</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">AncillaryData</span>
    <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">IPV6_RECVPKTINFO</span> <span class="ruby-comment"># RFC 3542</span>
      <span class="ruby-identifier">ipv6_recvpktinfo</span> = <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">IPV6_RECVPKTINFO</span>
    <span class="ruby-keyword">elsif</span> <span class="ruby-keyword">defined?</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">IPV6_PKTINFO</span> <span class="ruby-comment"># RFC 2292</span>
      <span class="ruby-identifier">ipv6_recvpktinfo</span> = <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">IPV6_PKTINFO</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">local_addrs</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">ip_address_list</span>

  <span class="ruby-identifier">ip_list</span> = []
  <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">host</span>, <span class="ruby-identifier">port</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">AI_PASSIVE</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">ai</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ipv4?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ip_address</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;0.0.0.0&quot;</span>
      <span class="ruby-identifier">local_addrs</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span>
        <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">ipv4?</span>
        <span class="ruby-identifier">ip_list</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">a</span>.<span class="ruby-identifier">to_sockaddr</span>, <span class="ruby-value">:INET</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>);
      }
    <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ipv6?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ip_address</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;::&quot;</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">ipv6_recvpktinfo</span>
      <span class="ruby-identifier">local_addrs</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span>
        <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">ipv6?</span>
        <span class="ruby-identifier">ip_list</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">a</span>.<span class="ruby-identifier">to_sockaddr</span>, <span class="ruby-value">:INET6</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>);
      }
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">ip_list</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">ai</span>
    <span class="ruby-keyword">end</span>
  }
  <span class="ruby-identifier">ip_list</span>.<span class="ruby-identifier">uniq!</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:to_sockaddr</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">port</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
    <span class="ruby-identifier">sockets</span> = <span class="ruby-identifier">ip_sockets_port0</span>(<span class="ruby-identifier">ip_list</span>, <span class="ruby-keyword">false</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">ip_list</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">ip</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">ai</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">udp</span>(<span class="ruby-identifier">ip</span>.<span class="ruby-identifier">ip_address</span>, <span class="ruby-identifier">port</span>)
      <span class="ruby-keyword">begin</span>
        <span class="ruby-identifier">s</span> = <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">bind</span>
      <span class="ruby-keyword">rescue</span> <span class="ruby-constant">SystemCallError</span>
        <span class="ruby-identifier">last_error</span> = <span class="ruby-identifier">$!</span>
        <span class="ruby-keyword">next</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">sockets</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">s</span>
    }
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">empty?</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-identifier">last_error</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">ai</span> = <span class="ruby-identifier">s</span>.<span class="ruby-identifier">local_address</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">ipv6_recvpktinfo</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ipv6?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">ai</span>.<span class="ruby-identifier">ip_address</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;::&quot;</span>
      <span class="ruby-identifier">s</span>.<span class="ruby-identifier">setsockopt</span>(<span class="ruby-value">:IPV6</span>, <span class="ruby-identifier">ipv6_recvpktinfo</span>, <span class="ruby-value">1</span>)
    <span class="ruby-keyword">end</span>
  }

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">sockets</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">sockets</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:close</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">sockets</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">sockets</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-unix" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">unix</span><span
                                class="method-args">(path) { |socket| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>creates a new socket connected to path using UNIX socket socket.</p>

<p>If a block is given, the block is called with the socket. The value of the block is returned. The socket is closed when this method returns.</p>

<p>If no block is given, the socket is returned.</p>

<pre class="ruby"><span class="ruby-comment"># talk to /tmp/sock socket.</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">unix</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sock</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">t</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> { <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">sock</span>, <span class="ruby-constant">STDOUT</span>) }
  <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-constant">STDIN</span>, <span class="ruby-identifier">sock</span>)
  <span class="ruby-identifier">t</span>.<span class="ruby-identifier">join</span>
}
</pre>

                              <div class="method-source-code" id="unix-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 1079</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">unix</span>(<span class="ruby-identifier">path</span>) <span class="ruby-comment"># :yield: socket</span>
  <span class="ruby-identifier">addr</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">unix</span>(<span class="ruby-identifier">path</span>)
  <span class="ruby-identifier">sock</span> = <span class="ruby-identifier">addr</span>.<span class="ruby-identifier">connect</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">sock</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">sock</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-unix_server_loop" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">unix_server_loop</span><span
                                class="method-args">(path) { |socket, client_addrinfo| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>creates a UNIX socket server on <em>path</em>. It calls the block for each socket accepted.</p>

<p>If <em>host</em> is specified, it is used with <em>port</em> to determine the server ports.</p>

<p>The socket is <strong>not</strong> closed when the block returns. So application should close it.</p>

<p>This method deletes the socket file pointed by <em>path</em> at first if the file is a socket file and it is owned by the user of the application. This is safe only if the directory of <em>path</em> is not changed by a malicious user. So don’t use /tmp/malicious-users-directory/socket. Note that /tmp/socket and /tmp/your-private-directory/socket is safe assuming that /tmp has sticky bit.</p>

<pre class="ruby"><span class="ruby-comment"># Sequential echo server.</span>
<span class="ruby-comment"># It services only one client at a time.</span>
<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">unix_server_loop</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sock</span>, <span class="ruby-identifier">client_addrinfo</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-constant">IO</span>.<span class="ruby-identifier">copy_stream</span>(<span class="ruby-identifier">sock</span>, <span class="ruby-identifier">sock</span>)
  <span class="ruby-keyword">ensure</span>
    <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-keyword">end</span>
}
</pre>

                              <div class="method-source-code" id="unix_server_loop-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 1166</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">unix_server_loop</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>) <span class="ruby-comment"># :yield: socket, client_addrinfo</span>
  <span class="ruby-identifier">unix_server_socket</span>(<span class="ruby-identifier">path</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">serv</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">accept_loop</span>(<span class="ruby-identifier">serv</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span>)
  }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-unix_server_socket" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">unix_server_socket</span><span
                                class="method-args">(path) { |s| ... }</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>creates a UNIX server socket on <em>path</em></p>

<p>If no block given, it returns a listening socket.</p>

<p>If a block is given, it is called with the socket and the block value is returned. When the block exits, the socket is closed and the socket file is removed.</p>

<pre class="ruby"><span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">unix_server_socket</span>(<span class="ruby-string">&quot;/tmp/s&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">socket</span>                  <span class="ruby-comment">#=&gt; #&lt;Socket:fd 3&gt;</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">local_address</span>    <span class="ruby-comment">#=&gt; #&lt;Addrinfo: /tmp/s SOCK_STREAM&gt;</span>

<span class="ruby-constant">Socket</span>.<span class="ruby-identifier">unix_server_socket</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span>                     <span class="ruby-comment">#=&gt; #&lt;Socket:fd 3&gt;</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">local_address</span>       <span class="ruby-comment">#=&gt; # #&lt;Addrinfo: /tmp/sock SOCK_STREAM&gt;</span>
}
</pre>

                              <div class="method-source-code" id="unix_server_socket-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 1109</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">unix_server_socket</span>(<span class="ruby-identifier">path</span>)
  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">unix_socket_abstract_name?</span>(<span class="ruby-identifier">path</span>)
    <span class="ruby-keyword">begin</span>
      <span class="ruby-identifier">st</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">lstat</span>(<span class="ruby-identifier">path</span>)
    <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">ENOENT</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">st</span>&amp;.<span class="ruby-identifier">socket?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">st</span>.<span class="ruby-identifier">owned?</span>
      <span class="ruby-constant">File</span>.<span class="ruby-identifier">unlink</span> <span class="ruby-identifier">path</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">s</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">unix</span>(<span class="ruby-identifier">path</span>).<span class="ruby-identifier">listen</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">s</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">s</span>.<span class="ruby-identifier">close</span>
      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">unix_socket_abstract_name?</span>(<span class="ruby-identifier">path</span>)
        <span class="ruby-constant">File</span>.<span class="ruby-identifier">unlink</span> <span class="ruby-identifier">path</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">s</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-unpack_sockaddr_in" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          unpack_sockaddr_in(sockaddr) &rarr; [port, ip_address]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Unpacks <em>sockaddr</em> into port and ip_address.</p>

<p><em>sockaddr</em> should be a string or an addrinfo for AF_INET/AF_INET6.</p>

<pre class="ruby"><span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&quot;127.0.0.1&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">sockaddr</span> <span class="ruby-comment">#=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">unpack_sockaddr_in</span>(<span class="ruby-identifier">sockaddr</span>) <span class="ruby-comment">#=&gt; [80, &quot;127.0.0.1&quot;]</span>
</pre>

                              <div class="method-source-code" id="unpack_sockaddr_in-source">
            <pre>static VALUE
sock_s_unpack_sockaddr_in(VALUE self, VALUE addr)
{
    struct sockaddr_in * sockaddr;
    VALUE host;

    sockaddr = (struct sockaddr_in*)SockAddrStringValuePtr(addr);
    if (RSTRING_LEN(addr) &lt;
        (char*)&amp;((struct sockaddr *)sockaddr)-&gt;sa_family +
        sizeof(((struct sockaddr *)sockaddr)-&gt;sa_family) -
        (char*)sockaddr)
        rb_raise(rb_eArgError, &quot;too short sockaddr&quot;);
    if (((struct sockaddr *)sockaddr)-&gt;sa_family != AF_INET
#ifdef INET6
        &amp;&amp; ((struct sockaddr *)sockaddr)-&gt;sa_family != AF_INET6
#endif
        ) {
#ifdef INET6
        rb_raise(rb_eArgError, &quot;not an AF_INET/AF_INET6 sockaddr&quot;);
#else
        rb_raise(rb_eArgError, &quot;not an AF_INET sockaddr&quot;);
#endif
    }
    host = rsock_make_ipaddr((struct sockaddr*)sockaddr, RSTRING_SOCKLEN(addr));
    return rb_assoc_new(INT2NUM(ntohs(sockaddr-&gt;sin_port)), host);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-unpack_sockaddr_un" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          unpack_sockaddr_un(sockaddr) &rarr; path
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Unpacks <em>sockaddr</em> into path.</p>

<p><em>sockaddr</em> should be a string or an addrinfo for <a href="Socket.html#AF_UNIX"><code>AF_UNIX</code></a>.</p>

<pre class="ruby"><span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_un</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">unpack_sockaddr_un</span>(<span class="ruby-identifier">sockaddr</span>) <span class="ruby-comment">#=&gt; &quot;/tmp/sock&quot;</span>
</pre>

                              <div class="method-source-code" id="unpack_sockaddr_un-source">
            <pre>static VALUE
sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
{
    struct sockaddr_un * sockaddr;
    VALUE path;

    sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr);
    if (RSTRING_LEN(addr) &lt;
        (char*)&amp;((struct sockaddr *)sockaddr)-&gt;sa_family +
        sizeof(((struct sockaddr *)sockaddr)-&gt;sa_family) -
        (char*)sockaddr)
        rb_raise(rb_eArgError, &quot;too short sockaddr&quot;);
    if (((struct sockaddr *)sockaddr)-&gt;sa_family != AF_UNIX) {
        rb_raise(rb_eArgError, &quot;not an AF_UNIX sockaddr&quot;);
    }
    if (sizeof(struct sockaddr_un) &lt; (size_t)RSTRING_LEN(addr)) {
        rb_raise(rb_eTypeError, &quot;too long sockaddr_un - %ld longer than %d&quot;,
                 RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un));
    }
    path = rsock_unixpath_str(sockaddr, RSTRING_SOCKLEN(addr));
    return path;
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="private-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Private Class Methods</h3>
                </header>

                  <div id="method-c-unix_socket_abstract_name-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">unix_socket_abstract_name?</span><span
                                class="method-args">(path)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="unix_socket_abstract_name-3F-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 1137</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">unix_socket_abstract_name?</span>(<span class="ruby-identifier">path</span>)
  <span class="ruby-regexp">/linux/</span> <span class="ruby-operator">=~</span> <span class="ruby-constant">RUBY_PLATFORM</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-regexp">/\A(\0|\z)/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">path</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-accept" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          accept &rarr; [client_socket, client_addrinfo]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Accepts a next connection. Returns a new <a href="Socket.html"><code>Socket</code></a> object and <a href="Addrinfo.html"><code>Addrinfo</code></a> object.</p>

<pre class="ruby"><span class="ruby-identifier">serv</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:INET</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">serv</span>.<span class="ruby-identifier">listen</span>(<span class="ruby-value">5</span>)
<span class="ruby-identifier">c</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:INET</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">c</span>.<span class="ruby-identifier">connect</span>(<span class="ruby-identifier">serv</span>.<span class="ruby-identifier">connect_address</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">serv</span>.<span class="ruby-identifier">accept</span> <span class="ruby-comment">#=&gt; [#&lt;Socket:fd 6&gt;, #&lt;Addrinfo: 127.0.0.1:48555 TCP&gt;]</span>
</pre>

                              <div class="method-source-code" id="accept-source">
            <pre>static VALUE
sock_accept(VALUE server)
{
    union_sockaddr buffer;
    socklen_t length = (socklen_t)sizeof(buffer);

    VALUE peer = rsock_s_accept(rb_cSocket, server, &amp;buffer.addr, &amp;length);

    return rb_assoc_new(peer, rsock_io_socket_addrinfo(peer, &amp;buffer.addr, length));
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-accept_nonblock" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          accept_nonblock([options]) &rarr; [client_socket, client_addrinfo]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the underlying file descriptor. It returns an array containing the accepted socket for the incoming connection, <em>client_socket</em>, and an <a href="Addrinfo.html"><code>Addrinfo</code></a>, <em>client_addrinfo</em>.</p>

<h3 id="method-i-accept_nonblock-label-Example">Example<span><a href="#method-i-accept_nonblock-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># In one script, start this first</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-identifier">sockaddr</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">listen</span>(<span class="ruby-value">5</span>)
<span class="ruby-keyword">begin</span> <span class="ruby-comment"># emulate blocking accept</span>
  <span class="ruby-identifier">client_socket</span>, <span class="ruby-identifier">client_addrinfo</span> = <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">accept_nonblock</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">WaitReadable</span>, <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EINTR</span>
  <span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>([<span class="ruby-identifier">socket</span>])
  <span class="ruby-keyword">retry</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;The client said, &#39;#{client_socket.readline.chomp}&#39;&quot;</span>
<span class="ruby-identifier">client_socket</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;Hello from script one!&quot;</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>

<span class="ruby-comment"># In another script, start this second</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">connect</span>(<span class="ruby-identifier">sockaddr</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;Hello from script 2.&quot;</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;The server said, &#39;#{socket.readline.chomp}&#39;&quot;</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>
</pre>

<p>Refer to <a href="Socket.html#method-i-accept"><code>Socket#accept</code></a> for the exceptions that may be thrown if the call to <em>accept_nonblock</em> fails.</p>

<p><a href="Socket.html#method-i-accept_nonblock"><code>Socket#accept_nonblock</code></a> may raise any error corresponding to accept(2) failure, including Errno::EWOULDBLOCK.</p>

<p>If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED or Errno::EPROTO, it is extended by IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for retrying accept_nonblock.</p>

<p>By specifying a keyword argument <em>exception</em> to <code>false</code>, you can indicate that <a href="Socket.html#method-i-accept_nonblock"><code>accept_nonblock</code></a> should not raise an IO::WaitReadable exception, but return the symbol <code>:wait_readable</code> instead.</p>

<h3 id="method-i-accept_nonblock-label-See">See<span><a href="#method-i-accept_nonblock-label-See">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><a href="Socket.html#method-i-accept"><code>Socket#accept</code></a></p>
</li></ul>

                              <div class="method-source-code" id="accept_nonblock-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 596</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">accept_nonblock</span>(<span class="ruby-value">exception:</span> <span class="ruby-keyword">true</span>)
  <span class="ruby-identifier">__accept_nonblock</span>(<span class="ruby-identifier">exception</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-bind" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          bind(local_sockaddr) &rarr; 0
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Binds to the given local address.</p>

<h3 id="method-i-bind-label-Parameter">Parameter<span><a href="#method-i-bind-label-Parameter">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><code>local_sockaddr</code> - the <code>struct</code> sockaddr contained in a string or an <a href="Addrinfo.html"><code>Addrinfo</code></a> object</p>
</li></ul>

<h3 id="method-i-bind-label-Example">Example<span><a href="#method-i-bind-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-comment"># use Addrinfo</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:INET</span>, <span class="ruby-value">:STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">tcp</span>(<span class="ruby-string">&quot;127.0.0.1&quot;</span>, <span class="ruby-value">2222</span>))
<span class="ruby-identifier">p</span> <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">local_address</span> <span class="ruby-comment">#=&gt; #&lt;Addrinfo: 127.0.0.1:2222 TCP&gt;</span>

<span class="ruby-comment"># use struct sockaddr</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>( <span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">bind</span>( <span class="ruby-identifier">sockaddr</span> )
</pre>

<h3 id="method-i-bind-label-Unix-based+Exceptions">Unix-based Exceptions<span><a href="#method-i-bind-label-Unix-based+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On unix-based based systems the following system exceptions may be raised if the call to <em>bind</em> fails:</p>
<ul><li>
<p>Errno::EACCES - the specified <em>sockaddr</em> is protected and the current user does not have permission to bind to it</p>
</li><li>
<p>Errno::EADDRINUSE - the specified <em>sockaddr</em> is already in use</p>
</li><li>
<p>Errno::EADDRNOTAVAIL - the specified <em>sockaddr</em> is not available from the local machine</p>
</li><li>
<p>Errno::EAFNOSUPPORT - the specified <em>sockaddr</em> is not a valid address for the family of the calling <code>socket</code></p>
</li><li>
<p>Errno::EBADF - the <em>sockaddr</em> specified is not a valid file descriptor</p>
</li><li>
<p>Errno::EFAULT - the <em>sockaddr</em> argument cannot be accessed</p>
</li><li>
<p>Errno::EINVAL - the <code>socket</code> is already bound to an address, and the protocol does not support binding to the new <em>sockaddr</em> or the <code>socket</code> has been shut down.</p>
</li><li>
<p>Errno::EINVAL - the address length is not a valid length for the address family</p>
</li><li>
<p>Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded PATH_MAX</p>
</li><li>
<p>Errno::ENOBUFS - no buffer space is available</p>
</li><li>
<p>Errno::ENOSR - there were insufficient STREAMS resources available to complete the operation</p>
</li><li>
<p>Errno::ENOTSOCK - the <code>socket</code> does not refer to a socket</p>
</li><li>
<p>Errno::EOPNOTSUPP - the socket type of the <code>socket</code> does not support binding to an address</p>
</li></ul>

<p>On unix-based based systems if the address family of the calling <code>socket</code> is <a href="Socket.html#AF_UNIX"><code>Socket::AF_UNIX</code></a> the follow exceptions may be raised if the call to <em>bind</em> fails:</p>
<ul><li>
<p>Errno::EACCES - search permission is denied for a component of the prefix path or write access to the <code>socket</code> is denied</p>
</li><li>
<p>Errno::EDESTADDRREQ - the <em>sockaddr</em> argument is a null pointer</p>
</li><li>
<p>Errno::EISDIR - same as Errno::EDESTADDRREQ</p>
</li><li>
<p>Errno::EIO - an i/o error occurred</p>
</li><li>
<p>Errno::ELOOP - too many symbolic links were encountered in translating the pathname in <em>sockaddr</em></p>
</li><li>
<p>Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX characters</p>
</li><li>
<p>Errno::ENOENT - a component of the pathname does not name an existing file or the pathname is an empty string</p>
</li><li>
<p>Errno::ENOTDIR - a component of the path prefix of the pathname in <em>sockaddr</em> is not a directory</p>
</li><li>
<p>Errno::EROFS - the name would reside on a read only filesystem</p>
</li></ul>

<h3 id="method-i-bind-label-Windows+Exceptions">Windows Exceptions<span><a href="#method-i-bind-label-Windows+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On Windows systems the following system exceptions may be raised if the call to <em>bind</em> fails:</p>
<ul><li>
<p>Errno::ENETDOWN– the network is down</p>
</li><li>
<p>Errno::EACCES - the attempt to connect the datagram socket to the broadcast address failed</p>
</li><li>
<p>Errno::EADDRINUSE - the socket’s local address is already in use</p>
</li><li>
<p>Errno::EADDRNOTAVAIL - the specified address is not a valid address for this computer</p>
</li><li>
<p>Errno::EFAULT - the socket’s internal address or address length parameter is too small or is not a valid part of the user space addressed</p>
</li><li>
<p>Errno::EINVAL - the <code>socket</code> is already bound to an address</p>
</li><li>
<p>Errno::ENOBUFS - no buffer space is available</p>
</li><li>
<p>Errno::ENOTSOCK - the <code>socket</code> argument does not refer to a socket</p>
</li></ul>

<h3 id="method-i-bind-label-See">See<span><a href="#method-i-bind-label-See">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p>bind manual pages on unix-based systems</p>
</li><li>
<p>bind function in Microsoft’s Winsock functions reference</p>
</li></ul>

                              <div class="method-source-code" id="bind-source">
            <pre>static VALUE
sock_bind(VALUE sock, VALUE addr)
{
    VALUE rai;
    rb_io_t *fptr;

    SockAddrStringValueWithAddrinfo(addr, rai);
    GetOpenFile(sock, fptr);
    if (bind(fptr-&gt;fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr)) &lt; 0)
        rsock_sys_fail_raddrinfo_or_sockaddr(&quot;bind(2)&quot;, addr, rai);

    return INT2FIX(0);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-connect" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          connect(remote_sockaddr) &rarr; 0
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Requests a connection to be made on the given <code>remote_sockaddr</code>. Returns 0 if successful, otherwise an exception is raised.</p>

<h3 id="method-i-connect-label-Parameter">Parameter<span><a href="#method-i-connect-label-Parameter">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><code>remote_sockaddr</code> - the <code>struct</code> sockaddr contained in a string or <a href="Addrinfo.html"><code>Addrinfo</code></a> object</p>
</li></ul>

<h3 id="method-i-connect-label-Example-3A">Example:<span><a href="#method-i-connect-label-Example-3A">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># Pull down Google&#39;s web page</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>( <span class="ruby-value">80</span>, <span class="ruby-string">&#39;www.google.com&#39;</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">connect</span>( <span class="ruby-identifier">sockaddr</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">write</span>( <span class="ruby-string">&quot;GET / HTTP/1.0\r\n\r\n&quot;</span> )
<span class="ruby-identifier">results</span> = <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">read</span>
</pre>

<h3 id="method-i-connect-label-Unix-based+Exceptions">Unix-based Exceptions<span><a href="#method-i-connect-label-Unix-based+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On unix-based systems the following system exceptions may be raised if the call to <em>connect</em> fails:</p>
<ul><li>
<p>Errno::EACCES - search permission is denied for a component of the prefix path or write access to the <code>socket</code> is denied</p>
</li><li>
<p>Errno::EADDRINUSE - the <em>sockaddr</em> is already in use</p>
</li><li>
<p>Errno::EADDRNOTAVAIL - the specified <em>sockaddr</em> is not available from the local machine</p>
</li><li>
<p>Errno::EAFNOSUPPORT - the specified <em>sockaddr</em> is not a valid address for the address family of the specified <code>socket</code></p>
</li><li>
<p>Errno::EALREADY - a connection is already in progress for the specified socket</p>
</li><li>
<p>Errno::EBADF - the <code>socket</code> is not a valid file descriptor</p>
</li><li>
<p>Errno::ECONNREFUSED - the target <em>sockaddr</em> was not listening for connections refused the connection request</p>
</li><li>
<p>Errno::ECONNRESET - the remote host reset the connection request</p>
</li><li>
<p>Errno::EFAULT - the <em>sockaddr</em> cannot be accessed</p>
</li><li>
<p>Errno::EHOSTUNREACH - the destination host cannot be reached (probably because the host is down or a remote router cannot reach it)</p>
</li><li>
<p>Errno::EINPROGRESS - the O_NONBLOCK is set for the <code>socket</code> and the connection cannot be immediately established; the connection will be established asynchronously</p>
</li><li>
<p>Errno::EINTR - the attempt to establish the connection was interrupted by delivery of a signal that was caught; the connection will be established asynchronously</p>
</li><li>
<p>Errno::EISCONN - the specified <code>socket</code> is already connected</p>
</li><li>
<p>Errno::EINVAL - the address length used for the <em>sockaddr</em> is not a valid length for the address family or there is an invalid family in <em>sockaddr</em></p>
</li><li>
<p>Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded PATH_MAX</p>
</li><li>
<p>Errno::ENETDOWN - the local interface used to reach the destination is down</p>
</li><li>
<p>Errno::ENETUNREACH - no route to the network is present</p>
</li><li>
<p>Errno::ENOBUFS - no buffer space is available</p>
</li><li>
<p>Errno::ENOSR - there were insufficient STREAMS resources available to complete the operation</p>
</li><li>
<p>Errno::ENOTSOCK - the <code>socket</code> argument does not refer to a socket</p>
</li><li>
<p>Errno::EOPNOTSUPP - the calling <code>socket</code> is listening and cannot be connected</p>
</li><li>
<p>Errno::EPROTOTYPE - the <em>sockaddr</em> has a different type than the socket bound to the specified peer address</p>
</li><li>
<p>Errno::ETIMEDOUT - the attempt to connect timed out before a connection was made.</p>
</li></ul>

<p>On unix-based systems if the address family of the calling <code>socket</code> is <a href="Socket.html#AF_UNIX"><code>AF_UNIX</code></a> the follow exceptions may be raised if the call to <em>connect</em> fails:</p>
<ul><li>
<p>Errno::EIO - an i/o error occurred while reading from or writing to the file system</p>
</li><li>
<p>Errno::ELOOP - too many symbolic links were encountered in translating the pathname in <em>sockaddr</em></p>
</li><li>
<p>Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX characters</p>
</li><li>
<p>Errno::ENOENT - a component of the pathname does not name an existing file or the pathname is an empty string</p>
</li><li>
<p>Errno::ENOTDIR - a component of the path prefix of the pathname in <em>sockaddr</em> is not a directory</p>
</li></ul>

<h3 id="method-i-connect-label-Windows+Exceptions">Windows Exceptions<span><a href="#method-i-connect-label-Windows+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On Windows systems the following system exceptions may be raised if the call to <em>connect</em> fails:</p>
<ul><li>
<p>Errno::ENETDOWN - the network is down</p>
</li><li>
<p>Errno::EADDRINUSE - the socket’s local address is already in use</p>
</li><li>
<p>Errno::EINTR - the socket was cancelled</p>
</li><li>
<p>Errno::EINPROGRESS - a blocking socket is in progress or the service provider is still processing a callback function. Or a nonblocking connect call is in progress on the <code>socket</code>.</p>
</li><li>
<p>Errno::EALREADY - see Errno::EINVAL</p>
</li><li>
<p>Errno::EADDRNOTAVAIL - the remote address is not a valid address, such as ADDR_ANY TODO check ADDRANY TO <a href="Socket.html#INADDR_ANY"><code>INADDR_ANY</code></a></p>
</li><li>
<p>Errno::EAFNOSUPPORT - addresses in the specified family cannot be used with with this <code>socket</code></p>
</li><li>
<p>Errno::ECONNREFUSED - the target <em>sockaddr</em> was not listening for connections refused the connection request</p>
</li><li>
<p>Errno::EFAULT - the socket’s internal address or address length parameter is too small or is not a valid part of the user space address</p>
</li><li>
<p>Errno::EINVAL - the <code>socket</code> is a listening socket</p>
</li><li>
<p>Errno::EISCONN - the <code>socket</code> is already connected</p>
</li><li>
<p>Errno::ENETUNREACH - the network cannot be reached from this host at this time</p>
</li><li>
<p>Errno::EHOSTUNREACH - no route to the network is present</p>
</li><li>
<p>Errno::ENOBUFS - no buffer space is available</p>
</li><li>
<p>Errno::ENOTSOCK - the <code>socket</code> argument does not refer to a socket</p>
</li><li>
<p>Errno::ETIMEDOUT - the attempt to connect timed out before a connection was made.</p>
</li><li>
<p>Errno::EWOULDBLOCK - the socket is marked as nonblocking and the connection cannot be completed immediately</p>
</li><li>
<p>Errno::EACCES - the attempt to connect the datagram socket to the broadcast address failed</p>
</li></ul>

<h3 id="method-i-connect-label-See">See<span><a href="#method-i-connect-label-See">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p>connect manual pages on unix-based systems</p>
</li><li>
<p>connect function in Microsoft’s Winsock functions reference</p>
</li></ul>

                              <div class="method-source-code" id="connect-source">
            <pre>static VALUE
sock_connect(VALUE sock, VALUE addr)
{
    VALUE rai;
    rb_io_t *fptr;
    int fd, n;

    SockAddrStringValueWithAddrinfo(addr, rai);
    addr = rb_str_new4(addr);
    GetOpenFile(sock, fptr);
    fd = fptr-&gt;fd;
    n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_SOCKLEN(addr), 0, NULL);
    if (n &lt; 0) {
        rsock_sys_fail_raddrinfo_or_sockaddr(&quot;connect(2)&quot;, addr, rai);
    }

    return INT2FIX(n);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-connect_nonblock" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          connect_nonblock(remote_sockaddr, [options]) &rarr; 0
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Requests a connection to be made on the given <code>remote_sockaddr</code> after O_NONBLOCK is set for the underlying file descriptor. Returns 0 if successful, otherwise an exception is raised.</p>

<h3 id="method-i-connect_nonblock-label-Parameter">Parameter<span><a href="#method-i-connect_nonblock-label-Parameter">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object</span>
</pre>

<h3 id="method-i-connect_nonblock-label-Example-3A">Example:<span><a href="#method-i-connect_nonblock-label-Example-3A">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># Pull down Google&#39;s web page</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">80</span>, <span class="ruby-string">&#39;www.google.com&#39;</span>)
<span class="ruby-keyword">begin</span> <span class="ruby-comment"># emulate blocking connect</span>
  <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">connect_nonblock</span>(<span class="ruby-identifier">sockaddr</span>)
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">WaitWritable</span>
  <span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>(<span class="ruby-keyword">nil</span>, [<span class="ruby-identifier">socket</span>]) <span class="ruby-comment"># wait 3-way handshake completion</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">connect_nonblock</span>(<span class="ruby-identifier">sockaddr</span>) <span class="ruby-comment"># check connection failure</span>
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Errno</span><span class="ruby-operator">::</span><span class="ruby-constant">EISCONN</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&quot;GET / HTTP/1.0\r\n\r\n&quot;</span>)
<span class="ruby-identifier">results</span> = <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">read</span>
</pre>

<p>Refer to <a href="Socket.html#method-i-connect"><code>Socket#connect</code></a> for the exceptions that may be thrown if the call to <em>connect_nonblock</em> fails.</p>

<p><a href="Socket.html#method-i-connect_nonblock"><code>Socket#connect_nonblock</code></a> may raise any error corresponding to connect(2) failure, including Errno::EINPROGRESS.</p>

<p>If the exception is Errno::EINPROGRESS, it is extended by IO::WaitWritable. So IO::WaitWritable can be used to rescue the exceptions for retrying connect_nonblock.</p>

<p>By specifying a keyword argument <em>exception</em> to <code>false</code>, you can indicate that <a href="Socket.html#method-i-connect_nonblock"><code>connect_nonblock</code></a> should not raise an IO::WaitWritable exception, but return the symbol <code>:wait_writable</code> instead.</p>

<h3 id="method-i-connect_nonblock-label-See">See<span><a href="#method-i-connect_nonblock-label-See">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># Socket#connect</span>
</pre>

                              <div class="method-source-code" id="connect_nonblock-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 1216</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">connect_nonblock</span>(<span class="ruby-identifier">addr</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">true</span>)
  <span class="ruby-identifier">__connect_nonblock</span>(<span class="ruby-identifier">addr</span>, <span class="ruby-identifier">exception</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-ipv6only-21" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">ipv6only!</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>enable the socket option <a href="Socket.html#IPV6_V6ONLY"><code>IPV6_V6ONLY</code></a> if <a href="Socket.html#IPV6_V6ONLY"><code>IPV6_V6ONLY</code></a> is available.</p>

                              <div class="method-source-code" id="ipv6only-21-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 467</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">ipv6only!</span>
  <span class="ruby-keyword">if</span> <span class="ruby-keyword">defined?</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">IPV6_V6ONLY</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">setsockopt</span>(<span class="ruby-value">:IPV6</span>, <span class="ruby-value">:V6ONLY</span>, <span class="ruby-value">1</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-listen" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          listen( int ) &rarr; 0
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Listens for connections, using the specified <code>int</code> as the backlog. A call to <em>listen</em> only applies if the <code>socket</code> is of type <a href="Socket.html#SOCK_STREAM"><code>SOCK_STREAM</code></a> or <a href="Socket.html#SOCK_SEQPACKET"><code>SOCK_SEQPACKET</code></a>.</p>

<h3 id="method-i-listen-label-Parameter">Parameter<span><a href="#method-i-listen-label-Parameter">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><code>backlog</code> - the maximum length of the queue for pending connections.</p>
</li></ul>

<h3 id="method-i-listen-label-Example+1">Example 1<span><a href="#method-i-listen-label-Example+1">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>( <span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">bind</span>( <span class="ruby-identifier">sockaddr</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">listen</span>( <span class="ruby-value">5</span> )
</pre>

<h3 id="method-i-listen-label-Example+2+-28listening+on+an+arbitrary+port-2C+unix-based+systems+only-29-3A">Example 2 (listening on an arbitrary port, unix-based systems only):<span><a href="#method-i-listen-label-Example+2+-28listening+on+an+arbitrary+port-2C+unix-based+systems+only-29-3A">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">listen</span>( <span class="ruby-value">1</span> )
</pre>

<h3 id="method-i-listen-label-Unix-based+Exceptions">Unix-based Exceptions<span><a href="#method-i-listen-label-Unix-based+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On unix based systems the above will work because a new <code>sockaddr</code> struct is created on the address ADDR_ANY, for an arbitrary port number as handed off by the kernel. It will not work on Windows, because Windows requires that the <code>socket</code> is bound by calling <em>bind</em> before it can <em>listen</em>.</p>

<p>If the <em>backlog</em> amount exceeds the implementation-dependent maximum queue length, the implementation’s maximum queue length will be used.</p>

<p>On unix-based based systems the following system exceptions may be raised if the call to <em>listen</em> fails:</p>
<ul><li>
<p>Errno::EBADF - the <em>socket</em> argument is not a valid file descriptor</p>
</li><li>
<p>Errno::EDESTADDRREQ - the <em>socket</em> is not bound to a local address, and the protocol does not support listening on an unbound socket</p>
</li><li>
<p>Errno::EINVAL - the <em>socket</em> is already connected</p>
</li><li>
<p>Errno::ENOTSOCK - the <em>socket</em> argument does not refer to a socket</p>
</li><li>
<p>Errno::EOPNOTSUPP - the <em>socket</em> protocol does not support listen</p>
</li><li>
<p>Errno::EACCES - the calling process does not have appropriate privileges</p>
</li><li>
<p>Errno::EINVAL - the <em>socket</em> has been shut down</p>
</li><li>
<p>Errno::ENOBUFS - insufficient resources are available in the system to complete the call</p>
</li></ul>

<h3 id="method-i-listen-label-Windows+Exceptions">Windows Exceptions<span><a href="#method-i-listen-label-Windows+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On Windows systems the following system exceptions may be raised if the call to <em>listen</em> fails:</p>
<ul><li>
<p>Errno::ENETDOWN - the network is down</p>
</li><li>
<p>Errno::EADDRINUSE - the socket’s local address is already in use. This usually occurs during the execution of <em>bind</em> but could be delayed if the call to <em>bind</em> was to a partially wildcard address (involving ADDR_ANY) and if a specific address needs to be committed at the time of the call to <em>listen</em></p>
</li><li>
<p>Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function</p>
</li><li>
<p>Errno::EINVAL - the <code>socket</code> has not been bound with a call to <em>bind</em>.</p>
</li><li>
<p>Errno::EISCONN - the <code>socket</code> is already connected</p>
</li><li>
<p>Errno::EMFILE - no more socket descriptors are available</p>
</li><li>
<p>Errno::ENOBUFS - no buffer space is available</p>
</li><li>
<p>Errno::ENOTSOC - <code>socket</code> is not a socket</p>
</li><li>
<p>Errno::EOPNOTSUPP - the referenced <code>socket</code> is not a type that supports the <em>listen</em> method</p>
</li></ul>

<h3 id="method-i-listen-label-See">See<span><a href="#method-i-listen-label-See">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p>listen manual pages on unix-based systems</p>
</li><li>
<p>listen function in Microsoft’s Winsock functions reference</p>
</li></ul>

                              <div class="method-source-code" id="listen-source">
            <pre>VALUE
rsock_sock_listen(VALUE sock, VALUE log)
{
    rb_io_t *fptr;
    int backlog;

    backlog = NUM2INT(log);
    GetOpenFile(sock, fptr);
    if (listen(fptr-&gt;fd, backlog) &lt; 0)
        rb_sys_fail(&quot;listen(2)&quot;);

    return INT2FIX(0);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-recvfrom" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          recvfrom(maxlen) &rarr; [mesg, sender_addrinfo]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          recvfrom(maxlen, flags) &rarr; [mesg, sender_addrinfo]
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Receives up to <em>maxlen</em> bytes from <code>socket</code>. <em>flags</em> is zero or more of the <code>MSG_</code> options. The first element of the results, <em>mesg</em>, is the data received. The second element, <em>sender_addrinfo</em>, contains protocol-specific address information of the sender.</p>

<h3 id="method-i-recvfrom-label-Parameters">Parameters<span><a href="#method-i-recvfrom-label-Parameters">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><code>maxlen</code> - the maximum number of bytes to receive from the socket</p>
</li><li>
<p><code>flags</code> - zero or more of the <code>MSG_</code> options</p>
</li></ul>

<h3 id="method-i-recvfrom-label-Example">Example<span><a href="#method-i-recvfrom-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># In one file, start this first</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>( <span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">bind</span>( <span class="ruby-identifier">sockaddr</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">listen</span>( <span class="ruby-value">5</span> )
<span class="ruby-identifier">client</span>, <span class="ruby-identifier">client_addrinfo</span> = <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">accept</span>
<span class="ruby-identifier">data</span> = <span class="ruby-identifier">client</span>.<span class="ruby-identifier">recvfrom</span>( <span class="ruby-value">20</span> )[<span class="ruby-value">0</span>].<span class="ruby-identifier">chomp</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;I only received 20 bytes &#39;#{data}&#39;&quot;</span>
<span class="ruby-identifier">sleep</span> <span class="ruby-value">1</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>

<span class="ruby-comment"># In another file, start this second</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>( <span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">connect</span>( <span class="ruby-identifier">sockaddr</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;Watch this get cut short!&quot;</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>
</pre>

<h3 id="method-i-recvfrom-label-Unix-based+Exceptions">Unix-based Exceptions<span><a href="#method-i-recvfrom-label-Unix-based+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On unix-based based systems the following system exceptions may be raised if the call to <em>recvfrom</em> fails:</p>
<ul><li>
<p>Errno::EAGAIN - the <code>socket</code> file descriptor is marked as O_NONBLOCK and no data is waiting to be received; or <a href="Socket.html#MSG_OOB"><code>MSG_OOB</code></a> is set and no out-of-band data is available and either the <code>socket</code> file descriptor is marked as O_NONBLOCK or the <code>socket</code> does not support blocking to wait for out-of-band-data</p>
</li><li>
<p>Errno::EWOULDBLOCK - see Errno::EAGAIN</p>
</li><li>
<p>Errno::EBADF - the <code>socket</code> is not a valid file descriptor</p>
</li><li>
<p>Errno::ECONNRESET - a connection was forcibly closed by a peer</p>
</li><li>
<p>Errno::EFAULT - the socket’s internal buffer, address or address length cannot be accessed or written</p>
</li><li>
<p>Errno::EINTR - a signal interrupted <em>recvfrom</em> before any data was available</p>
</li><li>
<p>Errno::EINVAL - the <a href="Socket.html#MSG_OOB"><code>MSG_OOB</code></a> flag is set and no out-of-band data is available</p>
</li><li>
<p>Errno::EIO - an i/o error occurred while reading from or writing to the filesystem</p>
</li><li>
<p>Errno::ENOBUFS - insufficient resources were available in the system to perform the operation</p>
</li><li>
<p>Errno::ENOMEM - insufficient memory was available to fulfill the request</p>
</li><li>
<p>Errno::ENOSR - there were insufficient STREAMS resources available to complete the operation</p>
</li><li>
<p>Errno::ENOTCONN - a receive is attempted on a connection-mode socket that is not connected</p>
</li><li>
<p>Errno::ENOTSOCK - the <code>socket</code> does not refer to a socket</p>
</li><li>
<p>Errno::EOPNOTSUPP - the specified flags are not supported for this socket type</p>
</li><li>
<p>Errno::ETIMEDOUT - the connection timed out during connection establishment or due to a transmission timeout on an active connection</p>
</li></ul>

<h3 id="method-i-recvfrom-label-Windows+Exceptions">Windows Exceptions<span><a href="#method-i-recvfrom-label-Windows+Exceptions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>On Windows systems the following system exceptions may be raised if the call to <em>recvfrom</em> fails:</p>
<ul><li>
<p>Errno::ENETDOWN - the network is down</p>
</li><li>
<p>Errno::EFAULT - the internal buffer and from parameters on <code>socket</code> are not part of the user address space, or the internal fromlen parameter is too small to accommodate the peer address</p>
</li><li>
<p>Errno::EINTR - the (blocking) call was cancelled by an internal call to the WinSock function WSACancelBlockingCall</p>
</li><li>
<p>Errno::EINPROGRESS - a blocking Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function</p>
</li><li>
<p>Errno::EINVAL - <code>socket</code> has not been bound with a call to <em>bind</em>, or an unknown flag was specified, or <a href="Socket.html#MSG_OOB"><code>MSG_OOB</code></a> was specified for a socket with <a href="Socket.html#SO_OOBINLINE"><code>SO_OOBINLINE</code></a> enabled, or (for byte stream-style sockets only) the internal len parameter on <code>socket</code> was zero or negative</p>
</li><li>
<p>Errno::EISCONN - <code>socket</code> is already connected. The call to <em>recvfrom</em> is not permitted with a connected socket on a socket that is connection oriented or connectionless.</p>
</li><li>
<p>Errno::ENETRESET - the connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress.</p>
</li><li>
<p>Errno::EOPNOTSUPP - <a href="Socket.html#MSG_OOB"><code>MSG_OOB</code></a> was specified, but <code>socket</code> is not stream-style such as type <a href="Socket.html#SOCK_STREAM"><code>SOCK_STREAM</code></a>. OOB data is not supported in the communication domain associated with <code>socket</code>, or <code>socket</code> is unidirectional and supports only send operations</p>
</li><li>
<p>Errno::ESHUTDOWN - <code>socket</code> has been shutdown. It is not possible to call <em>recvfrom</em> on a socket after <em>shutdown</em> has been invoked.</p>
</li><li>
<p>Errno::EWOULDBLOCK - <code>socket</code> is marked as nonblocking and a  call to <em>recvfrom</em> would block.</p>
</li><li>
<p>Errno::EMSGSIZE - the message was too large to fit into the specified buffer and was truncated.</p>
</li><li>
<p>Errno::ETIMEDOUT - the connection has been dropped, because of a network failure or because the system on the other end went down without notice</p>
</li><li>
<p>Errno::ECONNRESET - the virtual circuit was reset by the remote side executing a hard or abortive close. The application should close the socket; it is no longer usable. On a UDP-datagram socket this error indicates a previous send operation resulted in an ICMP Port Unreachable message.</p>
</li></ul>

                              <div class="method-source-code" id="recvfrom-source">
            <pre>static VALUE
sock_recvfrom(int argc, VALUE *argv, VALUE sock)
{
    return rsock_s_recvfrom(sock, argc, argv, RECV_SOCKET);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-recvfrom_nonblock" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          recvfrom_nonblock(maxlen[, flags[, outbuf[, opts]]]) &rarr; [mesg, sender_addrinfo]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Receives up to <em>maxlen</em> bytes from <code>socket</code> using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. <em>flags</em> is zero or more of the <code>MSG_</code> options. The first element of the results, <em>mesg</em>, is the data received. The second element, <em>sender_addrinfo</em>, contains protocol-specific address information of the sender.</p>

<p>When recvfrom(2) returns 0, <a href="Socket.html#method-i-recvfrom_nonblock"><code>Socket#recvfrom_nonblock</code></a> returns an empty string as data. The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.</p>

<h3 id="method-i-recvfrom_nonblock-label-Parameters">Parameters<span><a href="#method-i-recvfrom_nonblock-label-Parameters">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><code>maxlen</code> - the maximum number of bytes to receive from the socket</p>
</li><li>
<p><code>flags</code> - zero or more of the <code>MSG_</code> options</p>
</li><li>
<p><code>outbuf</code> - destination String buffer</p>
</li><li>
<p><code>opts</code> - keyword hash, supporting ‘exception: false`</p>
</li></ul>

<h3 id="method-i-recvfrom_nonblock-label-Example">Example<span><a href="#method-i-recvfrom_nonblock-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># In one file, start this first</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-identifier">sockaddr</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">listen</span>(<span class="ruby-value">5</span>)
<span class="ruby-identifier">client</span>, <span class="ruby-identifier">client_addrinfo</span> = <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">accept</span>
<span class="ruby-keyword">begin</span> <span class="ruby-comment"># emulate blocking recvfrom</span>
  <span class="ruby-identifier">pair</span> = <span class="ruby-identifier">client</span>.<span class="ruby-identifier">recvfrom_nonblock</span>(<span class="ruby-value">20</span>)
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">WaitReadable</span>
  <span class="ruby-constant">IO</span>.<span class="ruby-identifier">select</span>([<span class="ruby-identifier">client</span>])
  <span class="ruby-keyword">retry</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">data</span> = <span class="ruby-identifier">pair</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">chomp</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;I only received 20 bytes &#39;#{data}&#39;&quot;</span>
<span class="ruby-identifier">sleep</span> <span class="ruby-value">1</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>

<span class="ruby-comment"># In another file, start this second</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">sockaddr_in</span>(<span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">connect</span>(<span class="ruby-identifier">sockaddr</span>)
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;Watch this get cut short!&quot;</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>
</pre>

<p>Refer to <a href="Socket.html#method-i-recvfrom"><code>Socket#recvfrom</code></a> for the exceptions that may be thrown if the call to <em>recvfrom_nonblock</em> fails.</p>

<p><a href="Socket.html#method-i-recvfrom_nonblock"><code>Socket#recvfrom_nonblock</code></a> may raise any error corresponding to recvfrom(2) failure, including Errno::EWOULDBLOCK.</p>

<p>If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for retrying recvfrom_nonblock.</p>

<p>By specifying a keyword argument <em>exception</em> to <code>false</code>, you can indicate that <a href="Socket.html#method-i-recvfrom_nonblock"><code>recvfrom_nonblock</code></a> should not raise an IO::WaitReadable exception, but return the symbol <code>:wait_readable</code> instead.</p>

<h3 id="method-i-recvfrom_nonblock-label-See">See<span><a href="#method-i-recvfrom_nonblock-label-See">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><a href="Socket.html#method-i-recvfrom"><code>Socket#recvfrom</code></a></p>
</li></ul>

                              <div class="method-source-code" id="recvfrom_nonblock-source">
            <pre><span class="ruby-comment"># File socket/lib/socket.rb, line 539</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">recvfrom_nonblock</span>(<span class="ruby-identifier">len</span>, <span class="ruby-identifier">flag</span> = <span class="ruby-value">0</span>, <span class="ruby-identifier">str</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-value">exception:</span> <span class="ruby-keyword">true</span>)
  <span class="ruby-identifier">__recvfrom_nonblock</span>(<span class="ruby-identifier">len</span>, <span class="ruby-identifier">flag</span>, <span class="ruby-identifier">str</span>, <span class="ruby-identifier">exception</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-sysaccept" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sysaccept &rarr; [client_socket_fd, client_addrinfo]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Accepts an incoming connection returning an array containing the (integer) file descriptor for the incoming connection, <em>client_socket_fd</em>, and an <a href="Addrinfo.html"><code>Addrinfo</code></a>, <em>client_addrinfo</em>.</p>

<h3 id="method-i-sysaccept-label-Example">Example<span><a href="#method-i-sysaccept-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-comment"># In one script, start this first</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>( <span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">bind</span>( <span class="ruby-identifier">sockaddr</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">listen</span>( <span class="ruby-value">5</span> )
<span class="ruby-identifier">client_fd</span>, <span class="ruby-identifier">client_addrinfo</span> = <span class="ruby-identifier">socket</span>.<span class="ruby-identifier">sysaccept</span>
<span class="ruby-identifier">client_socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">for_fd</span>( <span class="ruby-identifier">client_fd</span> )
<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;The client said, &#39;#{client_socket.readline.chomp}&#39;&quot;</span>
<span class="ruby-identifier">client_socket</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;Hello from script one!&quot;</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>

<span class="ruby-comment"># In another script, start this second</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">Socket</span><span class="ruby-operator">::</span><span class="ruby-constant">Constants</span>
<span class="ruby-identifier">socket</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>( <span class="ruby-constant">AF_INET</span>, <span class="ruby-constant">SOCK_STREAM</span>, <span class="ruby-value">0</span> )
<span class="ruby-identifier">sockaddr</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">pack_sockaddr_in</span>( <span class="ruby-value">2200</span>, <span class="ruby-string">&#39;localhost&#39;</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">connect</span>( <span class="ruby-identifier">sockaddr</span> )
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;Hello from script 2.&quot;</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;The server said, &#39;#{socket.readline.chomp}&#39;&quot;</span>
<span class="ruby-identifier">socket</span>.<span class="ruby-identifier">close</span>
</pre>

<p>Refer to <a href="Socket.html#method-i-accept"><code>Socket#accept</code></a> for the exceptions that may be thrown if the call to <em>sysaccept</em> fails.</p>

<h3 id="method-i-sysaccept-label-See">See<span><a href="#method-i-sysaccept-label-See">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><a href="Socket.html#method-i-accept"><code>Socket#accept</code></a></p>
</li></ul>

                              <div class="method-source-code" id="sysaccept-source">
            <pre>static VALUE
sock_sysaccept(VALUE server)
{
    union_sockaddr buffer;
    socklen_t length = (socklen_t)sizeof(buffer);

    VALUE peer = rsock_s_accept(0, server, &amp;buffer.addr, &amp;length);

    return rb_assoc_new(peer, rsock_io_socket_addrinfo(peer, &amp;buffer.addr, length));
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

